Justin Gardner
Justin Gardner

Reputation: 625

One-liner for updating a value in a list of dictionaries - Python

I have a list of dictionaries like so:

l = [{"integer":"1"},{"integer":"2"},{"integer":"3"},{"integer":"4"}]

I would like to do something similar to the following so that each of the numbers which are the value pair for the "integer" key are returned as integers:

l = [{"integer":"1"},{"integer":"2"},{"integer":"3"},{"integer":"4"}]
r = map(lambda x: x["integer"]=int(x["integer"]), l)
print r 
#[{"integer":1},{"integer":2},{"integer":3},{"integer":4}]

But this causes an error:

SyntaxError: lambda cannot contain assignment

Does anyone know of a clean way to do this in python? Preferably a oneliner using map or something similar?

Upvotes: 4

Views: 6425

Answers (7)

Michael Seltenreich
Michael Seltenreich

Reputation: 3488

Use **kwargs to copy keyword arguments from the existing dicts, then overwrite the ones you want like so:

[{**element, 'value_to_overwrite':'new value'} for element in list_of_dicts]

Upvotes: 0

Hoàng Lê
Hoàng Lê

Reputation: 171

You can try the following

l = [{"integer":"1"},{"integer":"2"},{"integer":"3"},{"integer":"4"}]
a = [dict(d, **{'abcd':5}) for d in l]
print(a)
[{'integer': '1', 'abcd': 4}, {'integer': '2', 'abcd': 4}, {'integer': '3', 'abcd': 4}, {'integer': '4', 'abcd': 4}]

Upvotes: 0

pault
pault

Reputation: 43504

You should just use a loop:

l = [{"integer":"1"},{"integer":"2"},{"integer":"3"},{"integer":"4"}]
for d in l:
    d["integer"] = int(d["integer"])
print(l)
#[{'integer': 1}, {'integer': 2}, {'integer': 3}, {'integer': 4}]

However, here is a one-liner that should work for you:

l = [{"integer":"1"},{"integer":"2"},{"integer":"3"},{"integer":"4"}]
[d.update({"integer": int(d["integer"])}) for d in l]
print(l)
#[{'integer': 1}, {'integer': 2}, {'integer': 3}, {'integer': 4}]

Be aware that dict.update() returns None, so if you assigned the output of the list comprehension to a variable it would be a list containing all Nones.

print([d.update({"integer": int(d["integer"])}) for d in l])
#[None, None, None, None]

Upvotes: 3

Justin Gardner
Justin Gardner

Reputation: 625

Awesome solution by one of my friends in a chat:

>>> listOfDict = [{1:1338}, {1:1338}, {1:1338}]
>>> y = 1337
>>> value = 1
>>> map(lambda x: x.update({value: y}), listOfDict)
[None, None, None]
>>> listOfDict
[{1: 1337}, {1: 1337}, {1: 1337}]

Upvotes: 0

Jonas Wolff
Jonas Wolff

Reputation: 2244

[i.update({w:int(k)}) for i in l for w,k in i.items()]

it the second loop is only looping over one key set, so take the two loops with gram of salt :)

Upvotes: 1

Nick Dima
Nick Dima

Reputation: 348

Use a list comprehension comprehension

You will iterate through the dictionaries in the list and have them returned as x, then insert a new dictionary with your desired key and the integer value of the return within a new list

r = [{'integer': int(x['integer'])} for x in l]

Upvotes: 3

Joel
Joel

Reputation: 1574

The following works in one line:

r = [{'integer':int(x['integer'])} for x in l]
print(r)
# [{'integer': 1}, {'integer': 2}, {'integer': 3}, {'integer': 4}]

This utilizes a dict comprehension inside a list comprehension.

Upvotes: 1

Related Questions