python - list of dictionaries check key:value - python

UPDATE: Just to be clear, I'd like to check the key-value of the 'name' and 'last' and add only if these are not already in the list.
I have:
lst = [{'name':'John', 'last':'Smith'.... .... (other key-values)... },
{'name':'Will', 'last':'Smith'... ... (other key-values)... }]
I want to append a a new dict into this list only if it is not the exact same as an existing dictionary.
In other words:
dict1 = {'name':'John', 'last':'Smith'} # ==> wouldn't be appended
but...
dict2 = {'name':'John', 'last':'Brown'} # ==> WOULD be appended
Could someone explain the simplest way to do this, as well as in English, what is happening in the solution. THANKS!
Reference: Python: Check if any list element is a key in a dictionary

Since you asked for a way to only check the two keys, even if the dicts have other keys in them:
name_pairs = set((i['name'], i['last']) for i in lst)
if (d['name'], d['last']) not in name_pairs:
lst.append(d)

You can do it with this list comprehension just append everything to your list and run this:
lst.append(dict1)
lst.append(dict2)
[dict(y) for y in set(tuple(x.items()) for x in lst)]
The output is:
[
{'last': 'Smith', 'name': 'John'},
{'last': 'Brown', 'name': 'John'},
{'last': 'Smith', 'name': 'Will'}
]
With this method you can add extra fields and it will still work.

You could also write a small method to do it and return the list
def update_if_not_exist(lst, val):
if len([d for d in lst if (d['name'], d['last']) == (val['name'], val['last'])]) == 0:
lst.append(val)
return lst
lst = update_if_not_exist(lst, dict1)
lst = update_if_not_exist(lst, dict2)
It works by filtering the original list to matching the name and last keys and seeing if the result is empty.

>>> class Person(dict):
... def __eq__(self, other):
... return (self['first'] == other['first'] and
... self['second'] == other['second'])
... def __hash__(self):
... return hash((self['first'], self['second']))
>>> l = [{'first': 'John', 'second': 'Smith', 'age': 23},
... {'first': 'John', 'second': 'Smith', 'age': 30},
... {'first': 'Ann', 'second': 'Rice', 'age': 31}]
>>> l = set(map(Person, l))
>>> print l
set([{'first': 'Ann', 'second': 'Rice', 'age': 31},
{'first': 'John', 'second': 'Smith', 'age': 23}])
Instance of the Person class can be used as simple dict.

Related

Python: Way to build a dictionary with a variable key and append to a list as the value inside a loop

I have a list of dictionaries. I want to loop through this list of dictionary and for each specific name (an attribute inside each dictionary), I want to create a dictionary where the key is the name and the value of this key is a list which dynamically appends to the list in accordance with a specific condition.
For example, I have
d = [{'Name': 'John', 'id': 10},
{'Name': 'Mark', 'id': 21},
{'Name': 'Matthew', 'id': 30},
{'Name': 'Luke', 'id': 11},
{'Name': 'John', 'id': 20}]
I then built a list with only the names using names=[i['Name'] for i in dic1] so I have a list of names. Notice John will appear twice in this list (at the beginning and end). Then, I want to create a for-loop (for name in names), which creates a dictionary 'ID' that for its value is a list which appends this id field as it goes along.
So in the end I'm looking for this ID dictionary to have:
John: [10,20]
Mark: [21]
Matthew: [30]
Luke: [11]
Notice that John has a list length of two because his name appears twice in the list of dictionaries.
But I can't figure out a way to dynamically append these values to a list inside the for-loop. I tried:
ID={[]} #I also tried with just {}
for name in names:
ID[names].append([i['id'] for i in dic1 if i['Name'] == name])
Please let me know how one can accomplish this. Thanks.
Don't loop over the list of names and go searching for every one in the list; that's very inefficient, since you're scanning the whole list all over again for every name. Just loop over the original list once and update the ID dict as you go. Also, if you build the ID dict first, then you can get the list of names from it and avoid another list traversal:
names = ID.keys()
The easiest solution for ID itself is a dictionary with a default value of the empty list; that way ID[name].append will work for names that aren't in the dict yet, instead of blowing up with a KeyError.
from collections import defaultdict
ID = defaultdict(list)
for item in d:
ID[item['Name']].append(item['id'])
You can treat a defaultdict like a normal dict for almost every purpose, but if you need to, you can turn it into a plain dict by calling dict on it:
plain_id = dict(ID)
The Thonnu has a solution using get and list concatenation which works without defaultdict. Here's another take on a no-import solution:
ID = {}
for item in d:
name, number = item['Name'], item['id']
if name in ID:
ID[name].append(number)
else:
ID[name] = [ number ]
Using collections.defaultdict:
from collections import defaultdict
out = defaultdict(list)
for item in dic1:
out[item['Name']].append(item['id'])
print(dict(out))
Or, without any imports:
out = {}
for item in dic1:
out[item['Name']] = out.get(item['Name'], []) + [item['id']]
print(out)
Or, with a list comprehension:
out = {}
[out.update({item['Name']: out.get(item['Name'], []) + [item['id']]}) for item in dic1]
print(out)
Output:
{'John': [10, 20], 'Mark': [21], 'Matthew': [30], 'Luke': [11]}
dic1 = [{'Name': 'John', 'id': 10}, {'Name': 'Mark', 'id': 21}, {'Name': 'Matthew', 'id': 30}, {'Name': 'Luke', 'id': 11}, {'Name': 'John', 'id': 20}]
id_dict = {}
for dic in dic1:
key = dic['Name']
if key in id_dict:
id_dict[key].append(dic['id'])
else:
id_dict[key] = [dic['id']]
print(id_dict) # {'John': [10, 20], 'Mark': [21], 'Matthew': [30], 'Luke': [11]}
You can use defaultdict for this to initiate a dictionary with a default value. In this case the default value will be empty list.
from collections import defaultdict
d=defaultdict(list)
for item in dic1:
d[item['Name']].append(item['id'])
Output
{'John': [10, 20], 'Mark': [21], 'Matthew': [30], 'Luke': [11]} # by converting (not required) into pure dict dict(d)
You can do in a easy version
dic1=[{'Name': 'John', 'id':10}, {'Name': 'Mark', 'id':21},{'Name': 'Matthew', 'id':30}, {'Name': 'Luke', 'id':11}, {'Name': 'John', 'id':20}]
names=[i['Name'] for i in dic1]
ID = {}
for i, name in enumerate(names):
if name in ID:
ID[name].append(dic1[i]['id'])
else:
ID[name] = [dic1[i]['id']]
print(ID)

Create dictionary with keys from tuple and values from list of tuple

I am trying to create a dictionary with keys from x and values from a list of tuples 'users'. I need output in two formats: dict of lists & list of dict.
#input
users = [(2, "jhon", "jhon#company.com"),
(3, "mike", "mike#company.com"),
(4, "sam", "sam#company.com")]
x = ("id", "name", "email")
#expected output:
dict_of_lists = {'id': [2, 3, 4], 'name': ['jhon', 'mike', 'sam'], 'email': ['jhon#company.com', 'mike#company.com', 'sam#company.com']}
list_of_dicts = [{'id': 2, 'name': 'jhon', 'email': 'jhon#company.com'}, {'id': 3, 'name': 'mike', 'email': 'mike#company.com'}, {'id': 4, 'name': 'sam', 'email': 'same#company.com'}]
My code to generate dict of lists:
Dict = {}
def Add(key, value):
Dict[key] = value
ListId=[]
ListName=[]
ListEmail=[]
for i in range(len(users)):
ListId.append(users[i][0])
ListName.append(users[i][1])
ListEmail.append(users[i][2])
Add(x[0],ListId)
Add(x[1],ListName)
Add(x[2],ListEmail)
print(Dict)
My code to generate list of dict:
res = []
for i in range(len(users)):
Dict = {x[0] : users[i][0] , x[1] : users[i][1] , x[2]: users[i][2]}
res.append(Dict)
print(res)
I am looking for any other efficient solution to this question. Also, I have hardcoded index value from tuple x & list of tuple 'users' to access tuple value. How can I make code dynamic such that when a new key is added to x & value to users, it gets added to my output?
I have checked the web to find an answer but couldn't find anything similar to my question.
This is my first StackOverflow question. In case you have any suggestions for my question to write or ask in a better way then do let me know.
For the first one:
dict_of_lists = dict(zip(x, map(list, zip(*users))))
# or if tuples are fine
# dict_of_lists = dict(zip(x, zip(*users)))
output:
{'id': [2, 3, 4],
'name': ['jhon', 'mike', 'sam'],
'email': ['jhon#company.com', 'mike#company.com', 'sam#company.com']}
For the second:
list_of_dicts = [dict(zip(x,l)) for l in users]
output:
[{'id': 2, 'name': 'jhon', 'email': 'jhon#company.com'},
{'id': 3, 'name': 'mike', 'email': 'mike#company.com'},
{'id': 4, 'name': 'sam', 'email': 'sam#company.com'}]
Use list comprehension and dict comprehension:
list_of_dicts = [dict(zip(x,u)) for u in users]
dict_of_lists = {k: [u[i] for u in users] for i, k in enumerate(x)}

Check if a dict key is in a list (tuple) of dict

I'm using the following example to check if an item is in a list:
var = 'a'
var_list = ('a','b','c')
if var in var_list:
do_something()
But in my case, what I have is a dictionary and a list of dictionaries:
var = {'name': 'John', 'age': 35, 'city': 'Orlando'}
var_list = ( {'name': 'John', 'age': 36, 'city': 'Orlando'} , \
{'name': 'Alex', 'age': 22, 'city': 'New York'} , \
{'name': 'Celes', 'age': 24, 'city': 'Vector'} )
if var['name'] in var_list:
do_something()
I need to only check the key 'name' in the comparasion, otherwise if I do var in var_list the key age will be different and will result in not getting inside the if condition. Is it possible to compare only the name key?
Of course, I can iterate and check item by item, but if there is a function or something that will reduce the execution time, it will be great.
You can use a list comprehension:
if var['name'] in [d['name'] for d in var_list]:
doSomething()
One way is to test versus a set of names from your list of dictionaries.
from operator import itemgetter as iget
name_set = set(map(iget('name'), var_list))
if var['name'] in name_set:
do_something()
You can use builtin function any to check if there is a match. It will return boolean True or False based on whether the value is matched or not.
>>> from operator import itemgetter
>>> any(var['name'] == k for k in map(itemgetter('name'),var_list))
>>> True
However if you are interested in actual matched element you can do it with following generator expression:
>>> next((item for item in var_list if item["name"] == var["name"]),None)
>>> {'name': 'John', 'age': 36, 'city': 'Orlando'}

Get a unique set of dicts [duplicate]

Let's say I have a list of dictionaries:
[
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
How can I obtain a list of unique dictionaries (removing the duplicates)?
[
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
So make a temporary dict with the key being the id. This filters out the duplicates.
The values() of the dict will be the list
In Python2.7
>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> {v['id']:v for v in L}.values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
In Python3
>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> list({v['id']:v for v in L}.values())
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
In Python2.5/2.6
>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> dict((v['id'],v) for v in L).values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
The usual way to find just the common elements in a set is to use Python's set class. Just add all the elements to the set, then convert the set to a list, and bam the duplicates are gone.
The problem, of course, is that a set() can only contain hashable entries, and a dict is not hashable.
If I had this problem, my solution would be to convert each dict into a string that represents the dict, then add all the strings to a set() then read out the string values as a list() and convert back to dict.
A good representation of a dict in string form is JSON format. And Python has a built-in module for JSON (called json of course).
The remaining problem is that the elements in a dict are not ordered, and when Python converts the dict to a JSON string, you might get two JSON strings that represent equivalent dictionaries but are not identical strings. The easy solution is to pass the argument sort_keys=True when you call json.dumps().
EDIT: This solution was assuming that a given dict could have any part different. If we can assume that every dict with the same "id" value will match every other dict with the same "id" value, then this is overkill; #gnibbler's solution would be faster and easier.
EDIT: Now there is a comment from André Lima explicitly saying that if the ID is a duplicate, it's safe to assume that the whole dict is a duplicate. So this answer is overkill and I recommend #gnibbler's answer.
In case the dictionaries are only uniquely identified by all items (ID is not available) you can use the answer using JSON. The following is an alternative that does not use JSON, and will work as long as all dictionary values are immutable
[dict(s) for s in set(frozenset(d.items()) for d in L)]
Here's a reasonably compact solution, though I suspect not particularly efficient (to put it mildly):
>>> ds = [{'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30}
... ]
>>> map(dict, set(tuple(sorted(d.items())) for d in ds))
[{'age': 30, 'id': 2, 'name': 'hanna'}, {'age': 34, 'id': 1, 'name': 'john'}]
You can use numpy library (works for Python2.x only):
import numpy as np
list_of_unique_dicts=list(np.unique(np.array(list_of_dicts)))
To get it worked with Python 3.x (and recent versions of numpy), you need to convert array of dicts to numpy array of strings, e.g.
list_of_unique_dicts=list(np.unique(np.array(list_of_dicts).astype(str)))
a = [
{'id':1,'name':'john', 'age':34},
{'id':1,'name':'john', 'age':34},
{'id':2,'name':'hanna', 'age':30},
]
b = {x['id']:x for x in a}.values()
print(b)
outputs:
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
Since the id is sufficient for detecting duplicates, and the id is hashable: run 'em through a dictionary that has the id as the key. The value for each key is the original dictionary.
deduped_dicts = dict((item["id"], item) for item in list_of_dicts).values()
In Python 3, values() doesn't return a list; you'll need to wrap the whole right-hand-side of that expression in list(), and you can write the meat of the expression more economically as a dict comprehension:
deduped_dicts = list({item["id"]: item for item in list_of_dicts}.values())
Note that the result likely will not be in the same order as the original. If that's a requirement, you could use a Collections.OrderedDict instead of a dict.
As an aside, it may make a good deal of sense to just keep the data in a dictionary that uses the id as key to begin with.
We can do with pandas
import pandas as pd
yourdict=pd.DataFrame(L).drop_duplicates().to_dict('r')
Out[293]: [{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
Notice slightly different from the accept answer.
drop_duplicates will check all column in pandas , if all same then the row will be dropped .
For example :
If we change the 2nd dict name from john to peter
L=[
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'peter', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
pd.DataFrame(L).drop_duplicates().to_dict('r')
Out[295]:
[{'age': 34, 'id': 1, 'name': 'john'},
{'age': 34, 'id': 1, 'name': 'peter'},# here will still keeping the dict in the out put
{'age': 30, 'id': 2, 'name': 'hanna'}]
There are a lot of answers here, so let me add another:
import json
from typing import List
def dedup_dicts(items: List[dict]):
dedupped = [ json.loads(i) for i in set(json.dumps(item, sort_keys=True) for item in items)]
return dedupped
items = [
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
dedup_dicts(items)
I have summarized my favorites to try out:
https://repl.it/#SmaMa/Python-List-of-unique-dictionaries
# ----------------------------------------------
# Setup
# ----------------------------------------------
myList = [
{"id":"1", "lala": "value_1"},
{"id": "2", "lala": "value_2"},
{"id": "2", "lala": "value_2"},
{"id": "3", "lala": "value_3"}
]
print("myList:", myList)
# -----------------------------------------------
# Option 1 if objects has an unique identifier
# -----------------------------------------------
myUniqueList = list({myObject['id']:myObject for myObject in myList}.values())
print("myUniqueList:", myUniqueList)
# -----------------------------------------------
# Option 2 if uniquely identified by whole object
# -----------------------------------------------
myUniqueSet = [dict(s) for s in set(frozenset(myObject.items()) for myObject in myList)]
print("myUniqueSet:", myUniqueSet)
# -----------------------------------------------
# Option 3 for hashable objects (not dicts)
# -----------------------------------------------
myHashableObjects = list(set(["1", "2", "2", "3"]))
print("myHashAbleList:", myHashableObjects)
In python 3, simple trick, but based on unique field (id):
data = [ {'id': 1}, {'id': 1}]
list({ item['id'] : item for item in data}.values())
I don't know if you only want the id of your dicts in the list to be unique, but if the goal is to have a set of dict where the unicity is on all keys' values.. you should use tuples key like this in your comprehension :
>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... {'id':2,'name':'hanna', 'age':50}
... ]
>>> len(L)
4
>>> L=list({(v['id'], v['age'], v['name']):v for v in L}.values())
>>>L
[{'id': 1, 'name': 'john', 'age': 34}, {'id': 2, 'name': 'hanna', 'age': 30}, {'id': 2, 'name': 'hanna', 'age': 50}]
>>>len(L)
3
Hope it helps you or another person having the concern....
Expanding on John La Rooy (Python - List of unique dictionaries) answer, making it a bit more flexible:
def dedup_dict_list(list_of_dicts: list, columns: list) -> list:
return list({''.join(row[column] for column in columns): row
for row in list_of_dicts}.values())
Calling Function:
sorted_list_of_dicts = dedup_dict_list(
unsorted_list_of_dicts, ['id', 'name'])
If there is not a unique id in the dictionaries, then I'd keep it simple and define a function as follows:
def unique(sequence):
result = []
for item in sequence:
if item not in result:
result.append(item)
return result
The advantage with this approach, is that you can reuse this function for any comparable objects. It makes your code very readable, works in all modern versions of Python, preserves the order in the dictionaries, and is fast too compared to its alternatives.
>>> L = [
... {'id': 1, 'name': 'john', 'age': 34},
... {'id': 1, 'name': 'john', 'age': 34},
... {'id': 2, 'name': 'hanna', 'age': 30},
... ]
>>> unique(L)
[{'id': 1, 'name': 'john', 'age': 34}, {'id': 2, 'name': 'hanna', 'age': 30}]
In python 3.6+ (what I've tested), just use:
import json
#Toy example, but will also work for your case
myListOfDicts = [{'a':1,'b':2},{'a':1,'b':2},{'a':1,'b':3}]
#Start by sorting each dictionary by keys
myListOfDictsSorted = [sorted(d.items()) for d in myListOfDicts]
#Using json methods with set() to get unique dict
myListOfUniqueDicts = list(map(json.loads,set(map(json.dumps, myListOfDictsSorted))))
print(myListOfUniqueDicts)
Explanation: we're mapping the json.dumps to encode the dictionaries as json objects, which are immutable. set can then be used to produce an iterable of unique immutables. Finally, we convert back to our dictionary representation using json.loads. Note that initially, one must sort by keys to arrange the dictionaries in a unique form. This is valid for Python 3.6+ since dictionaries are ordered by default.
Well all the answers mentioned here are good, but in some answers one can face error if the dictionary items have nested list or dictionary, so I propose simple answer
a = [str(i) for i in a]
a = list(set(a))
a = [eval(i) for i in a]
Objects can fit into sets. You can work with objects instead of dicts and if needed after all set insertions convert back to a list of dicts. Example
class Person:
def __init__(self, id, age, name):
self.id = id
self.age = age
self.name = name
my_set = {Person(id=2, age=3, name='Jhon')}
my_set.add(Person(id=3, age=34, name='Guy'))
my_set.add({Person(id=2, age=3, name='Jhon')})
# if needed convert to list of dicts
list_of_dict = [{'id': obj.id,
'name': obj.name,
'age': obj.age} for obj in my_set]
A quick-and-dirty solution is just by generating a new list.
sortedlist = []
for item in listwhichneedssorting:
if item not in sortedlist:
sortedlist.append(item)
Let me add mine.
sort target dict so that {'a' : 1, 'b': 2} and {'b': 2, 'a': 1} are not treated differently
make it as json
deduplicate via set (as set does not apply to dicts)
again, turn it into dict via json.loads
import json
[json.loads(i) for i in set([json.dumps(i) for i in [dict(sorted(i.items())) for i in target_dict]])]
There may be more elegant solutions, but I thought it might be nice to add a more verbose solution to make it easier to follow. This assumes there is not a unique key, you have a simple k,v structure, and that you are using a version of python that guarantees list order. This would work for the original post.
data_set = [
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
# list of keys
keys = [k for k in data_set[0]]
# Create a List of Lists of the values from the data Set
data_set_list = [[v for v in v.values()] for v in data_set]
# Dedupe
new_data_set = []
for lst in data_set_list:
# Check if list exists in new data set
if lst in new_data_set:
print(lst)
continue
# Add list to new data set
new_data_set.append(lst)
# Create dicts
new_data_set = [dict(zip(keys,lst)) for lst in new_data_set]
print(new_data_set)
Pretty straightforward option:
L = [
{'id':1,'name':'john', 'age':34},
{'id':1,'name':'john', 'age':34},
{'id':2,'name':'hanna', 'age':30},
]
D = dict()
for l in L: D[l['id']] = l
output = list(D.values())
print output
Heres an implementation with little memory overhead at the cost of not being as compact as the rest.
values = [ {'id':2,'name':'hanna', 'age':30},
{'id':1,'name':'john', 'age':34},
{'id':1,'name':'john', 'age':34},
{'id':2,'name':'hanna', 'age':30},
{'id':1,'name':'john', 'age':34},]
count = {}
index = 0
while index < len(values):
if values[index]['id'] in count:
del values[index]
else:
count[values[index]['id']] = 1
index += 1
output:
[{'age': 30, 'id': 2, 'name': 'hanna'}, {'age': 34, 'id': 1, 'name': 'john'}]
This is the solution I found:
usedID = []
x = [
{'id':1,'name':'john', 'age':34},
{'id':1,'name':'john', 'age':34},
{'id':2,'name':'hanna', 'age':30},
]
for each in x:
if each['id'] in usedID:
x.remove(each)
else:
usedID.append(each['id'])
print x
Basically you check if the ID is present in the list, if it is, delete the dictionary, if not, append the ID to the list

Alter values of dictionaries which have the same keys in a list

I had this:
[{'name': 'Peter'}, {'name': 'Anna'}]
And I wanted to make this out of it:
[{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]
So I did:
>>> li = [{'name': 'Peter'}, {'name': 'Anna'}]
>>> new_li = []
>>> dic = {}
>>> for i in li:
... dic["name"] = i["name"] + " Williams"
... new_li.append(dic)
But:
>>> new_li
[{'name': 'Anna Williams'}, {'name': 'Anna Williams'}]
Why?
Could you also show how to best get [{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]?
Edit
The reason why I didn't understand this behavior is because I assumed that:
>>> dict = {'name':'Peter'}
>>> lis = [dict]
>>> dict['name'] = 'Olaf'
Where
>>> print lis
gives
[{'name': 'Peter'}]
While it actually is
[{'name': 'Olaf'}]
Because you're using the same dictionary object in each iteration.
On the second iteration, you are altering the value that you assigned to the name key in the previous iteration, and your list ends up containing two references to the same object.
I'd strongly recommend checking out the "Python Tutor" tool (pythontutor.com), which allows you to visualise the execution of some python code, and see what objects are being created in the stack. e.g. Python Tutor with your code
The correct way to do what you wanted would be:
li = [{'name': 'Peter'}, {'name': 'Anna'}]
new_li = []
for p in li:
new_p = {'name': p['name'] + ' Williams'}
new_li.append(new_p)
This way a new dictionary object is created with each iteration.
A more concise solution:
li = [{'name': 'Peter'}, {'name': 'Anna'}]
new_li = [{'name': p['name'] + ' Williams'} for p in li]
You need to create a new dictionary on each iteration of the loop, otherwise on each iteration you modify and append the same object to new_li:
>>> li = [{'name': 'Peter'}, {'name': 'Anna'}]
>>> new_li = []
>>> for i in li:
... new_li.append({'name': i['name'] + ' Williams'})
...
>>> new_li
[{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]
I'm assuming you want to alter it in-place. You don't need to make a new dict.
for d in inlist:
d["name"] += " Williams"
Is all you have to do.
You have run into a really nasty problem. The problem is with your dic. You are iterating over the list of names, and first you hit the one with 'Peter'. At this point, you set dic to {'name': 'Peter Williams'} and append it. But then, you hit the one with 'Anna' and change that very same dic to {'name': 'Anna Williams'}. So you end up with the same dictionary in your list twice. To fix this, you will need to do this instead:
new_li = []
for i in li:
n_li.append({'name': i['name'] + ' Williams'})

Categories