Sc4r
Sc4r

Reputation: 700

Looping through a dictionary

I'm trying to loop through a dictionary, and starting from the first key, it looks its value and loops through each element in the list and doubles it. Once it's done with the list it adds the key and the value to a new dictionary and then continues to the next key in the dictionary and continues the process. The value that is attached with each key will always be a list. Preferably without importing any modules.

Some input's and outputs to better understand what the code is supposed to be doing(the order will be always be different, so sometimes you'll have 'b' first or 'a' first.):

>>> create_dict({'a': [1, 2], 'b': [3, 4]})
{'a': ['1', '1', '2', '2'], 'b': ['3', '3', '4', '4']}
>>> create_dict({'a': ['c', 'd'], 'b': ['d', 'e']})
{'a': ['c', 'c', 'd', 'd'], 'b': ['d', 'd', 'e', 'e']}  
>>> create_dict({'a': ['e', 'f'], 'b': ['g', 'h']})
{'a': ['e', 'e', 'f', 'f'], 'b': ['g', 'g', 'h', 'h']}  

What I've written so far:

def create_dict(sample_dict):
    '''(dict) -> dict

    Given a dictionary, loop through the value in the first key and double
    each element in the list and add the result to a new dictionary, move on
    to the next key and continue the process.

    >>> create_dict({'a': [1, 2], 'b': [3, 4]})
    {'a': ['1', '1', '2', '2'], 'b': ['3', '3', '4', '4']}
    >>> create_dict({'a': ['c', 'd'], 'b': ['d', 'e']})
    {'a': ['c', 'c', 'd', 'd'], 'b': ['d', 'd', 'e', 'e']}
    >>> create_dict({'name': ['bob', 'smith'], 'last': ['jones', 'li']})
    {'name': ['bob', 'bob', 'smith', 'smith'], 'last': ['jones', 'jones', 'li', 'li']}

    '''
    new_dict = {}
    new_list = []
    for index in sample_dict.values():
        for element in index:
            new_list.extend([element] * 2)
    return new_dict

However the result I'm getting does not quite match what I had in mind:

>>> create_dict({'name': ['bob', 'smith'], 'last': ['jones', 'li']})
{'last': ['jones', 'jones', 'li', 'li', 'bob', 'bob', 'smith', 'smith'], 'name': ['jones', 'jones', 'li', 'li', 'bob', 'bob', 'smith', 'smith']}
>>> create_dict({'a': [1, 2], 'b': [3, 4]})
{'b': [3, 3, 4, 4, 1, 1, 2, 2], 'a': [3, 3, 4, 4, 1, 1, 2, 2]}

Thank you for those who help :)

Upvotes: 2

Views: 192

Answers (3)

iruvar
iruvar

Reputation: 23394

This can be a lot simpler with dictionary comprehensions

d = {'a': ['c', 'd'], 'b': ['d', 'e']}
{key:[y for z in zip(value, value) for y in z] for (key, value) in d.items()}
{'a': ['c', 'c', 'd', 'd'], 'b': ['d', 'd', 'e', 'e']}

Upvotes: 1

HennyH
HennyH

Reputation: 7944

def create_dict(sample_dict):
    new_dict = {} #build dict straight
    for key,value in sample_dict.items(): #.items() returns tuples: (key,val)
        new_list = [] #start with a new list for each pair in the dict
        for element in value: #go over each element in 'val'
            new_list.extend([element,element])
        new_dict[key] = new_list
    return new_dict

print create_dict({'name': ['bob', 'smith'], 'last': ['jones', 'li']})

Outputs:

>>> 
{'last': ['jones', 'jones', 'li', 'li'], 'name': ['bob', 'bob', 'smith', 'smith']}

Upvotes: 0

user2497586
user2497586

Reputation:

I think you're initializing your new_list too soon. It's grabbing too much data

So, try this:

def create_dict(sample_dict):
new_dict = {}
for key in sample_dict:
    new_list = []
    for val in sample_dict[key]:
        new_list.extend([val] * 2)  
    new_dict[key] = new_list
return new_dict

print create_dict({'a': [1, 2], 'b': [3, 4]})

It returns {'a': [1, 1, 2, 2], 'b': [3, 3, 4, 4]}

Upvotes: 1

Related Questions