Modifying list of dictionary using list comprehension

Multi tool use
Modifying list of dictionary using list comprehension
So I have the following list of dictionary
myList = [{'one':1, 'two':2,'three':3},
{'one':4, 'two':5,'three':6},
{'one':7, 'two':8,'three':9}]
This is just an example of a dictionary that I have. My question is, it possible to somehow modify say key two
in all the dictionary to become twice their value, using list comprehension ?
two
I know how to use list comprehension to create new list of dictionary, but don't know how to modify them, I have come up with something like this
new_list = { <some if condiftion> for (k,v) in x.iteritems() for x in myList }
I am not sure how to specify a condition in the <some if condiftion>
, also is the nested list comprehension format I am thinking of correct ?
<some if condiftion>
I want the final output as per my example like this
[ {'one':1, 'two':4,'three':3},{'one':4, 'two':10,'three':6},{'one':7, 'two':16,'three':9} ]
What you created is a dictionary comprehension, not a list comprehension. Well, so far as
{
and [
are concerned– roganjosh
Jun 30 at 14:08
{
[
@roganjosh Sorry I got the terminology wrong. Huge fan of your name btw :)
– Mohammed Kashif
Jun 30 at 14:09
5 Answers
5
Use list comprehension with nested dict comprehension:
new_list = [{ k: v * 2 if k == 'two' else v for k,v in x.items()} for x in myList]
print (new_list)
[{'one': 1, 'two': 4, 'three': 3},
{'one': 4, 'two': 10, 'three': 6},
{'one': 7, 'two': 16, 'three': 9}]
Thanks! thats exactly what I needed :)
– Mohammed Kashif
Jun 30 at 14:10
myList = [ {'one':1, 'two':2,'three':3},{'one':4, 'two':5,'three':6},{'one':7, 'two':8,'three':9} ]
[ { k: 2*i[k] if k == 'two' else i[k] for k in i } for i in myList ]
[{'one': 1, 'three': 3, 'two': 4}, {'one': 4, 'three': 6, 'two': 10}, {'one': 7, 'three': 9, 'two': 16}]
A simple for
loop should be sufficient. However, if you want to use a dictionary comprehension, I find defining a mapping dictionary more readable and extendable than ternary statements:
for
factor = {'two': 2}
res = [{k: v*factor.get(k, 1) for k, v in d.items()} for d in myList]
print(res)
[{'one': 1, 'two': 4, 'three': 3},
{'one': 4, 'two': 10, 'three': 6},
{'one': 7, 'two': 16, 'three': 9}]
Thanks! I didn't knew we could use mapping like this!
– Mohammed Kashif
Jun 30 at 14:24
@MohammedKashif, Yep, the main point is for one condition an
if
/ else
ternary is fine. But it will become (more) unreadable if you have further conditions.– jpp
Jun 30 at 14:25
if
else
In python 3.5+ you can use the new unpacking syntax in dict literals introduced in PEP 448. This creates a copy of each dict and then overwrites the value for the key two
:
two
new_list = [{**d, 'two': d['two']*2} for d in myList]
# result:
# [{'one': 1, 'two': 4, 'three': 3},
# {'one': 4, 'two': 10, 'three': 6},
# {'one': 7, 'two': 16, 'three': 9}]
Hello did you tried this :
for d in myList:
d.update((k, v*2) for k, v in d.iteritems() if k == "two")
Thanks
Why not use
d['two'] = d['two'] * 2
instead of that d.update(...)
thing?– Aran-Fey
Jun 30 at 15:00
d['two'] = d['two'] * 2
d.update(...)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
List comprehension is used to build new lists, not modify old ones.
– Scott Hunter
Jun 30 at 14:07