user8212515
user8212515

Reputation: 1

Create a list from strings-list in Python

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.

Upvotes: 0

Views: 3570

Answers (6)

s3n0
s3n0

Reputation: 636

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': []}

Upvotes: 1

Mykola Zotko
Mykola Zotko

Reputation: 17911

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', [])])

Upvotes: 3

Eshita Shukla
Eshita Shukla

Reputation: 811

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': []}

Upvotes: 1

syntax-punk
syntax-punk

Reputation: 4620

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!

Upvotes: 1

Ohad Chaet
Ohad Chaet

Reputation: 519

@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])

Upvotes: 1

ForceBru
ForceBru

Reputation: 44906

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.

Upvotes: 1

Related Questions