0

I have a list in Python:

['first', 'second', 'foo']

I want to create a list of lists named after the list elements:

newlist = ['first':[], 'second':[], 'foo':[]]

I have seen some proposals that use Dictionaries, but when I tried to do it with OrderedDict, I lost the order of the elements in the creation.

Thanks in advance.

5
  • Which version of python are you using? Commented Mar 3, 2019 at 13:07
  • version 2.7.14. Commented Mar 3, 2019 at 13:26
  • 1
    If you want something (like 'first') to be associated with something (like a list), you have key-value-pairs. These are usually implemendet as dict ({'first': [], 'second': []}) but if you really want a list, then I recommend a list of tuples ([('first', []), ('second', [])]). Commented Mar 3, 2019 at 13:35
  • How did you create the OrderedDict. It's sole purpose is to retain the order in which you insert the elements. Commented Mar 3, 2019 at 13:42
  • I declared : newlist= collections.OrderedDict() and later: used newlist= {i:[] for i in signal_list}. I get {'second':[], 'foo':[], 'first':[]} (i.e. different ordering) Commented Mar 3, 2019 at 13:50

6 Answers 6

3

You can use the method fromkeys():

l = ['first', 'second', 'foo']

dict.fromkeys(l, [])
# {'first': [], 'second': [], 'foo': []}

In Python 3.6 and below use OrderedDict instead of dict:

from collections import OrderedDict

l = ['first', 'second', 'foo']
OrderedDict.fromkeys(l, [])
# OrderedDict([('first', []), ('second', []), ('foo', [])])
Sign up to request clarification or add additional context in comments.

Comments

1

Since Python 3.7 regular Python's dicts are ordered:

>>> dict((name, []) for name in ['first', 'second', 'third'])
{'first': [], 'second': [], 'third': []}

dicts in CPython 3.6 are also ordered, but it's an implementation detail.

1 Comment

{name: [] for name in ['first', 'second', 'third']} would be a lot more natural.
1

@ForceBru gave a nice answer for Python 3.7 (I learned myself), but for lower versions that would work:

from collections import OrderedDict
l = ['first', 'second', 'foo']
d = OrderedDict([(x, []) for x in l])

2 Comments

This one created [('first',[]), ('second',[]),('third',[])]. Not what I wanted
No, this one created OrderedDict([('first', []), ('second', []), ('foo', [])]). This is an OrderedDict object and not a list, don't confuse them.
1

The elements in the array you wanna end up having must be proper objects and the format that you've displayed in the example, doesn't make a lot of sense, but you can try to use dictionary elements inside your array where each elemnt has key (e.i 'foo') and value (i.e '[]'). So you will end with something like this:

newlist = [{'first':[]}, {'second':[]}, {'foo':[]}]

Now if you are happy with that, here is a map function with an anonymous lambda function which is gonna convert your initial array:

simplelist = ['first', 'second', 'foo']
newlist = list(map(lambda item: {item:[]}, simplelist))

Hope, you got your answer.

Cheers!

Comments

1

The structure that you have indicated, is a dictionary dict. The structure looks like:

test_dictionary = {'a':1, 'b':2, 'c':3}

# To access an element
print(test_dictionary['a'])   # Prints 1

To create a dictionary, as per your requirement:

test_dictionary = dict((name, []) for name in ['first', 'second', 'foo'])
print(test_dictionary)

The above line of code gives the following output:

{'first': [], 'second': [], 'foo': []}

Comments

1

The first problem is that you refer to the term "list", but you mean it as a word concept, not as a data type in Python language. The second problem is that the result will no longer represent the data type <list>, but the data type of the <dict> (dictionary). A simple one-line for can convert your variable-type <list> to the desired dictionary-type variable. It works in Python 2.7.x

>>> l = ['first', 'second', 'foo']
>>> type(l)
<type 'list'>
>>> d = {x:[] for x in l}
>>> type(d)
<type 'dict'>
>>> d
{'second': [], 'foo': [], 'first': []}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.