I have this list of dictionary:
MylistOfdict = [{'Word': 'surveillance',
'Word No': 1},
{'Word': 'equivocal',
'Word No': 2}]
I want to create a new list of dictionary (word_db2
) that has 3 dictionaries for each dictionary in MylistOfdict
. In addition to key and values of MylistOfdict, each of those dictionary should have 'Card Type' key with value Type 1, Type 2, Type 3 and 'Card Key' key with incremental value
Code:
word_db2 = []
key = 1
for i in MylistOfdict:
for j in range(1, 4):
i['Card Type'] = 'Type '+str(j)
i['Card Key'] = key
print(i)
word_db2.append(i)
key += 1
Output:
{'Word No': 1, 'Card Key': 1, 'Word': 'surveillance', 'Card Type': 'Type 1'}
{'Word No': 1, 'Card Key': 2, 'Word': 'surveillance', 'Card Type': 'Type 2'}
{'Word No': 1, 'Card Key': 3, 'Word': 'surveillance', 'Card Type': 'Type 3'}
{'Word No': 2, 'Card Key': 4, 'Word': 'equivocal', 'Card Type': 'Type 1'}
{'Word No': 2, 'Card Key': 5, 'Word': 'equivocal', 'Card Type': 'Type 2'}
{'Word No': 2, 'Card Key': 6, 'Word': 'equivocal', 'Card Type': 'Type 3'}
This output is correct, but word_db2
stores only last appended value in every iteration
print(word_db2)
Output:
[{'Card Key': 3, 'Card Type': 'Type 3', 'Word': 'surveillance', 'Word No': 1},
{'Card Key': 3, 'Card Type': 'Type 3', 'Word': 'surveillance', 'Word No': 1},
{'Card Key': 3, 'Card Type': 'Type 3', 'Word': 'surveillance', 'Word No': 1},
{'Card Key': 6, 'Card Type': 'Type 3', 'Word': 'equivocal', 'Word No': 2},
{'Card Key': 6, 'Card Type': 'Type 3', 'Word': 'equivocal', 'Word No': 2},
{'Card Key': 6, 'Card Type': 'Type 3', 'Word': 'equivocal', 'Word No': 2}]
Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.
Standard dict objects preserve order in the reference (CPython) implementations of Python 3.5 and 3.6, and this order-preserving property is becoming a language feature in Python 3.7.
Lists are mutable data types in Python. Lists is a 0 based index datatype meaning the index of the first element starts at 0. Lists are used to store multiple items in a single variable. Lists are one of the 4 data types present in Python i.e. Lists, Dictionary, Tuple & Set.
In Python, to iterate the dictionary ( dict ) with a for loop, use keys() , values() , items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() . Use the following dictionary as an example. You can iterate keys by using the dictionary object directly in a for loop.
Let's review the loop body logic step by step:
So the key point you missed is that you modify and append the same object that was selected on the first step. And at the end of the snippet word_db2
contains six object refs, but only two unique. As a result, the output shows similar rows.
You can make a shallow copy of a dict before modifying and appending it:
for j in range(1, 4):
i = dict(i)
i['Card Type'] = 'Type '+str(j)
i['Card Key'] = key
print(i)
word_db2.append(i)
key += 1
As further note, if the dict contains other mutable objects like nested dicts, you should make a deep copy:
import copy
old_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
new_dict = copy.deepcopy(old_dict)
old_dict['a'][1] = 7
new_dict['a'][1] # 2
When you append a dictionary to a list, a reference to the original object itself is appended. So, you are currently just modifying the existing object's keys and values in each iteration of the inner loop, so the last written value is the only thing which persists.
To do what you require, you would need to create a new dictionary object in each iteration of the inner loop. For the shown dictionaries in MylistOfdict, a simple dictionary comprehension would work. But if you have more complex dictionaries, use the copy module's deepcopy method.
MylistOfdict = [{'Word': 'surveillance', 'Word No': 1},
{'Word': 'equivocal', 'Word No': 2}]
word_db2 = []
key = 1
for i in MylistOfdict:
for j in range(1, 4):
# Creating a new dictionary object and copying keys and values from i
new_dict = {k: v for k, v in i.items()}
new_dict['Card Type'] = 'Type '+str(j)
new_dict['Card Key'] = key
print(new_dict)
word_db2.append(new_dict)
key += 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With