James C.
James C.

Reputation: 531

Mapping a list as dictionary values

I have a list of strings called lst which contains name, city, and email addresses for three people in such order. I have a dictionary called data that only contains keys.

lst = ['James','New York','[email protected]','Matt','San Francisco','[email protected]','Jessica','Los Angeles','[email protected]']

data = {
            "Name": None,
            "City": None,
            "email": None
        }

I would like to map lst items as values in data and produce a new list of dictionaries as below,

newlst = [{
            "Name": "James",
            "City": "New York",
            "email": "[email protected]"
        },
        {
            "Name": "Matt",
            "City": "San Francisco",
            "email": "[email protected]"
        },
        {
            "Name": "Jessica",
            "City": "Los Angeles",
            "email": "[email protected]"
        }]

I know how to add a single value to a specific key in a dictionary like data["Name"] = "James" but how would I achieve this by using list/dict comprehension or iteration?

Upvotes: 0

Views: 2083

Answers (2)

stuartnox
stuartnox

Reputation: 659

Here it is.

lst = ['James','New York','[email protected]','Matt','San Francisco','[email protected]','Jessica','Los Angeles','[email protected]']
newlst = []

for i in xrange( 0, len(lst), 3 ):
    d = {}
    d['Name'] = lst[i]
    d['City'] = lst[i+1]
    d['Email'] = lst[i+2]

    newlst.append( d )

print newlst

Output:

[{'City': 'New York', 'Email': '[email protected]', 'Name': 'James'},
 {'City': 'San Francisco', 'Email': '[email protected]', 'Name': 'Matt'},
 {'City': 'Los Angeles', 'Email': '[email protected]', 'Name': 'Jessica'}]

Using comprehension:

lst = ['James','New York','[email protected]','Matt','San Francisco','[email protected]','Jessica','Los Angeles','[email protected]']
newlst = [{'Name':lst[i], 'City':lst[i+1], 'Email':lst[i+2]} for i in xrange(0,len(lst),3)]

Upvotes: 4

Karin
Karin

Reputation: 8600

Here's an approach using list comprehension. The idea is that we create a dictionary based on a zip of the keys and three values of a time.

lst = [
    'James', 'New York', '[email protected]',
    'Matt', 'San Francisco', '[email protected]',
    'Jessica', 'Los Angeles', '[email protected]',
]

keys = [
    'Name',
    'City',
    'email',
]

newlst = [
    dict(zip(keys, values)) for values in [iter(lst)] * len(keys)
]
print(newlst)

Output:

[
    {'City': 'New York', 'Name': 'James', 'email': '[email protected]'},
    {'City': 'San Francisco', 'Name': 'Matt', 'email': '[email protected]'},
    {'City': 'Los Angeles', 'Name': 'Jessica', 'email': '[email protected]'}
]

[iter(lst)] * len(keys) will return chunks of 3 values at a time from the list. zip(keys, values) will then produce an iterator of tuples containing a key and corresponding value. Finally, dict() will turn this into a dictionary to be inserted into newlst. This loops until the list is exhausted.

Upvotes: 6

Related Questions