Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return copies of dictionary modified

Tags:

python

lambda

I have a dictionary and for a particular key, I have say 5 possible new values. So I am trying to create 5 copies of the original dictionary by using a simple lambda function that will replace the value of that particular key and return a copy of the master dictionary.

# This is the master dictionary.
d = {'fn' : 'Joseph', 'ln' : 'Randall', 'phone' : '100' }
# Joseph has got 4 other phone numbers
lst = ['200', '300', '400', '500']
# I want 4 copies of the dictionary d with these different phone numbers
# Later I would want to do some processing with those dictionary without affecting d

So I am trying to do this:

# y is the list I want to hold these copies of dictionaries with modified values
i = d.copy()
y = map( lambda x : (i.update({'phone' : x})) and i, lst )

I thought this would return a list of dictionaries and each of them would have phone number changed to 200, 300, 400 and 500 respectively. I can put a loop and create copies and change them using a naive approach, but I want to explore and know how I can exploit the lambdas to accomplish this.

Thanks in advance.

like image 791
Vijay Shankar Kalyanaraman Avatar asked Mar 06 '12 18:03

Vijay Shankar Kalyanaraman


People also ask

What method used to return a copy of the dictionary?

The dict. copy() method returns a shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any change is made in the copied dictionary will also reflect in the original dictionary.

How can you create a copy of a dictionary modifying the copy should not change the original )?

Use copy() This is a built-in Python function that can be used to create a shallow copy of a dictionary. This function takes no arguments and returns a shallow copy of the dictionary. When a change is made to the shallow copy, the original dictionary will remain unchanged.

What does dict items () method return?

Python Dictionary items() Method The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.

What are the ways to copy a dictionary d2 to d1?

Copy a dictionary with a for loop To copy a dictionary it is also possible to use a for loop: >>> d1 = {'a':1,'b':2} >>> d2 = {} >>> for key in d1: ... d2[key] = d1[key] ...


1 Answers

You can use a list comprehension:

>>> d = {'fn' : 'Joseph', 'ln' : 'Randall', 'phone' : '100' }
>>> lst = ['200', '300', '400', '500']
>>> [dict(d, phone=x) for x in lst]
[{'ln': 'Randall', 'phone': '200', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '300', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '400', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '500', 'fn': 'Joseph'}]

If you still insist on using map and a lambda (which does exactly the same, only a bit slower):

>>> map(lambda x: dict(d, phone=x), lst)
[{'ln': 'Randall', 'phone': '200', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '300', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '400', 'fn': 'Joseph'}, {'ln': 'Randall', 'phone': '500', 'fn': 'Joseph'}]

By the way, the reason why your approach didn't work as expected is because .update() modifies the dictionary in place, rather than creating a new dictionary that reflects the update. It also doesn't return the result, so the lambda evaluates to None (and you probably got back a list like [None, None, None, None].

like image 81
Niklas B. Avatar answered Oct 11 '22 08:10

Niklas B.