0

Is there a way to create multiple lists with names from elements of another list.

Eg:

names=["Rob","Mark","Steve"]

Is there a way to create lists like:

Rob=[]
Mark=[]
Steve=[]
Vignesh
  • 912
  • 6
  • 13
  • 24
  • http://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-in-python-via-a-while-loop – Ashalynd Jul 24 '14 at 14:54

2 Answers2

4

The one obvious way is like this:

>>> names = ["Rob","Mark","Steve"]
>>> lists = {name: [] for name in names}
>>> print lists
{'Steve': [], 'Rob': [], 'Mark': []}
wim
  • 338,267
  • 99
  • 616
  • 750
  • Thank you. That helped.. And its list a list inside a list to add elements to the lists and display them.. – Vignesh Jul 24 '14 at 14:58
0

You can use the exec statement with a string as argument. It will parse the string as a suite of Python statements.

names=["Rob","Mark","Steve"]
S = "".join([n +'= [];' for n in names])
exec(S)


In [1]: print Rob, Mark, Steve
[] [] []
DavidK
  • 2,495
  • 3
  • 23
  • 38