6

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 ?

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 ?

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}  ]
2
  • List comprehension is used to build new lists, not modify old ones. Commented Jun 30, 2018 at 14:07
  • 3
    What you created is a dictionary comprehension, not a list comprehension. Well, so far as { and [ are concerned Commented Jun 30, 2018 at 14:08

5 Answers 5

8

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}]
Sign up to request clarification or add additional context in comments.

Comments

3

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:

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}]

Comments

1
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}]

Comments

1

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:

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}]

2 Comments

Thanks! I didn't knew we could use mapping like this!
@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.
0

Hello did you tried this :

for d in myList:
  d.update((k, v*2) for k, v in d.iteritems() if k == "two")

Thanks

1 Comment

Why not use d['two'] = d['two'] * 2 instead of that d.update(...) thing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.