3

(I searched on this website + internet, but couldn't find an answer)

I am starting from:

myList = ['a', 'b', 'c']

And I want to get something pythonic equivalent of

a = []
b = []
c = []

or

a, b, c = ([] for i in range(3))

That is I want to create as many lists as elements in the original list and use (assign?) the elements in the original list as names for the list created.

Do you have suggestions, please?

tagoma
  • 3,896
  • 4
  • 38
  • 57

5 Answers5

6

A dictionary sounds perfect for this situation

Code:

myList = ['a', 'b', 'c']
dict_var=dict()
for key in myList:
    dict_var[key]=list()
print dict_var

Output:

{'a': [], 'c': [], 'b': []}

Notes:

  • You can still do all the things you do with variable list like append,extend etc.,.
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • Wow, don't know `dict.fromkeys()` before. Looks like `collections.defaultdict()`. – Remi Guan Nov 24 '15 at 12:37
  • 1
    @KevinGuan kind of it is just used to create list but you can still add a key of different value other than `list type` when you create from `fromkey` – The6thSense Nov 24 '15 at 12:39
  • 2
    @VigneshKalai `fromkeys` should not be used with mutable objects, unless it is absolutely necessary. It just uses the same object for all the keys, so all the values are one and same list. – thefourtheye Nov 24 '15 at 12:41
  • @thefourtheye can you please elaborate I cannot understand what you are saying – The6thSense Nov 24 '15 at 12:45
  • @thefourtheye is correct. To see it do: `d = {}.fromkeys(['a', 'b'], [])` and then `d['a'].append(1)`. – Aske Doerge Nov 24 '15 at 13:10
  • @AskeDoerge yes agreed :) – The6thSense Nov 24 '15 at 13:12
  • 1
    @KevinGuan see this http://stackoverflow.com/questions/23397153/append-value-to-one-list-in-dictionary-appends-value-to-all-lists-in-dictionary/23397182#23397182 you will get a little insight – The6thSense Nov 24 '15 at 13:13
  • It would be nice if the downvoter commented (which will not happen) why ? – The6thSense Nov 25 '15 at 07:06
6

Instead of creating new variables, you can simply create a dictionary of lists, and then you can access the individual lists with the name, like this

>>> myList = ['a', 'b', 'c']
>>> myDict = {item: [] for item in myList}
>>> myDict
{'c': [], 'a': [], 'b': []}

Now, you can use the lists, like this

>>> myDict['a'].append(1)
>>> myDict['b'].append(2)
>>> myDict['c'].append(3)
>>> myDict
{'c': [3], 'a': [1], 'b': [2]}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
5

Pythonic way is to use dictionary

myDict = {'a': [], 'b':[], 'c':[]}

myDict['a'].append("hello world")
print myDict['a'][0] # hello world

You can use list to create it

myList = ['a', 'b', 'c']

myDict = {}
for x in myList:
   myDict[x] = []
furas
  • 134,197
  • 12
  • 106
  • 148
1

What you asked can be achieved with:

myList = ['a', 'b', 'c']
for item in myList:
    eval("{} = []".format(item))

but maybe you should use a dictionary of lists like the other guys mentioned.

See this too How can you dynamically create variables via a while loop?

Community
  • 1
  • 1
Cătălin Matei
  • 151
  • 1
  • 1
  • 7
1

Thanks all for your inputs. After some more search on the internet, I came out with:

myList = ['a', 'b', 'c']
for item in myList:
 exec(item + ' = []')

Which gets exactly what I was after (and that can surely be written more compactedly).

tagoma
  • 3,896
  • 4
  • 38
  • 57