Tahnoon Pasha
Tahnoon Pasha

Reputation: 6018

How to set a value by key for a dictionary in python using the map function

I know that I can set a key-value pair by using

dict[key] = value

but I have a very long list of dicts of the type

dict = [{a:1, b:2, c:3, d:4},
        {a:2, b:3, c:4, d:5},
        {a:5, b:7, c:3, d:9}]

and I'd like to do something along the lines of

dict = map(lambda x: x['d'] <- x['d'] -1, dict)

how would I go about this? (This is a very simplified example so I'm not really trying to just subtract a number from all items by a particular key)

expected output would be in this case and not the general case I'm looking for

[{a:1, b:2, c:3, d:3},
 {a:2, b:3, c:4, d:4},
 {a:5, b:7, c:3, d:8}]

EDIT: 2

I believe the following does not work - so any similar solution would be helpful:

dict = map(lambda x: x.update(d, x[d] - 1), dict)

Upvotes: 0

Views: 104

Answers (5)

Borodin
Borodin

Reputation: 126722

map is a way of transforming an iterable to a list by performing the same operation on every item from the iterable. I don't think that's what you want to do here, and it has confused you.

On the face of it (although you haven't mentioned what the real operation is that you want to perform) a simple for is all that is necessary:

dict_list = [
    {'a': 1, 'b': 2, 'c': 3, 'd': 4},
    {'a': 2, 'b': 3, 'c': 4, 'd': 5},
    {'a': 5, 'b': 7, 'c': 3, 'd': 9},
]

for d in dict_list:
    d['d'] -= 1
    print(d)

output

{'a': 1, 'b': 2, 'c': 3, 'd': 3}
{'a': 2, 'b': 3, 'c': 4, 'd': 4}
{'a': 5, 'b': 7, 'c': 3, 'd': 8}

Upvotes: 2

Hackaholic
Hackaholic

Reputation: 19743

how about this: as exactly you said

>>> dicts = [{'a':1, 'b':2, 'c':3, 'd':4},
         {'a':2, 'b':3, 'c':4, 'd':5},
         {'a':5, 'b':7, 'c':3, 'd':9}]
>>> map(lambda x:x.update([('d',x['d']-1)]),dicts)
[None, None, None]
>>> dicts
[{'a': 1, 'c': 3, 'b': 2, 'd': 3}, {'a': 2, 'c': 4, 'b': 3, 'd': 4}, {'a': 5, 'c': 3, 'b': 7, 'd': 8}]

update will update the dictionary with (key,value) pair. Returns None

Upvotes: 2

falsetru
falsetru

Reputation: 369134

Using dict.__setitem__ and temporary list (or any other collection typer) trick:

>>> dicts = [{'a':1, 'b':2, 'c':3, 'd':4},
...          {'a':2, 'b':3, 'c':4, 'd':5},
...          {'a':5, 'b':7, 'c':3, 'd':9}]
>>> map(lambda d: [d.__setitem__('d', d['d'] - 1), d][1], dicts)
[{'a': 1, 'c': 3, 'b': 2, 'd': 3},
 {'a': 2, 'c': 4, 'b': 3, 'd': 4},
 {'a': 5, 'c': 3, 'b': 7, 'd': 8}]

Using simple for loop is moe recommended way. Especially there's a side effect in the function.

BTW, don't use dict as a variable name. It will shadows builtin function/type dict.

Upvotes: 0

user308827
user308827

Reputation: 21981

How about this:

my_dict = {k: f(v) for k, v in my_dict.iteritems()}

where f is whatever function you want.

Upvotes: -1

inspectorG4dget
inspectorG4dget

Reputation: 113975

dicts = [{'a':1, 'b':2, 'c':3, 'd':4},
         {'a':2, 'b':3, 'c':4, 'd':5},
         {'a':5, 'b':7, 'c':3, 'd':9}]

for d in dicts:
    d['d'] -= 1

Output:

In [94]: dicts
Out[94]: 
[{'d': 3, 'b': 2, 'c': 3, 'a': 1},
 {'d': 4, 'b': 3, 'c': 4, 'a': 2},
 {'d': 8, 'b': 7, 'c': 3, 'a': 5}]

Upvotes: 3

Related Questions