Python 3 is adding elements to a list regardless of key used in dict

python dictionary append
python dictionary methods
python list
python 3 dictionary
python dictionary keys
python dictionary of dictionaries
python dictionary get
python list of dictionaries

I have a dict that I defined with:

keys = ['Valid', 'Reverse']
NOC = dict.fromkeys(keys, [])

I then iterate through a file and based on elements in the rows I add them to different lists in that dict based on a value in the row

This is code inside the list comprehension looping through the reader content:

if item[0] == '4':
    if float(item[10]) < 0:
        state = 'Reverse'
        print(state)
        NOC[state].append(item[6]+"FOOOO")
    if float(item[10]) >= 0:
        state = 'Valid'
        print(state)
        NOC[state].append(item[6])

    print (NOC['Reverse'])

As you can see, I am adding the string "FOOOO" in the case of 'Reverse". I did this to see what was happening inside of the lists in the dict.

I have validated that the if-then statements are all working correctly.

However, the appends seem to be writing to the same place in memory. It's strange. Here is some output of the list comprehension looping through the file content:

Valid
['68382011714']
Valid
['68382011714', '69315013701']
Valid
['68382011714', '69315013701', '16729018201']
Valid
['68382011714', '69315013701', '16729018201', '54458098010']
Valid
['68382011714', '69315013701', '16729018201', '54458098010', '61570007301']
Valid
['68382011714', '69315013701', '16729018201', '54458098010', '61570007301', '61570007301']
Reverse
['68382011714', '69315013701', '16729018201', '54458098010', '61570007301', '61570007301', '61570007301FOOOO']

It makes no sense to me that print(NOC['Reverse']) is showing values that have anything other than those with the appended "FOOOO". Why is this happening?

You're creating only a single list with:

NOC = dict.fromkeys(keys, [])

which gets assigned to every key of NOC. Since the keys point to the same list, whatever key you use you'll get the same list back, hence you'll be modifying the same list. You can easily test that with:

assert NOC["Valid"] is not NOC["Reverse"], "It's the same list!"

Instead of doing that, make sure each key gets its own list as:

NOC = {k: [] for k in keys}

Dictionary Data Structures in Python 3, Dictionaries map keys to values, making key-value pairs that can then store data. In this tutorial, we will go over the dictionary data structure in Python. The returned format of this is a list made up of (key, value) tuple pairs with the We can use this method for adding key-value pairs to dictionaries with user-input. Add New Element With New Key in Dictionary Using Python. You can also add new items to the Dictionary in Python. To perform adding new items, you have to use the update() function of Python. The function takes Dictionary item as the argument to pass which you want to append.

The problem is because the value for both the keys has same reference, when you create a dictionary from keys using NOC = dict.fromkeys(keys, [])

>>> keys = ['Valid', 'Reverse']
>>> NOC = dict.fromkeys(keys, [])
>>> NOC
{'Valid': [], 'Reverse': []}
>>> id(NOC['Valid'])
50173312
>>> id(NOC['Reverse'])
50173312

If you can see here both keys has reference to the same list. So when you are appending it is getting appended to the same list. So, I would prefer you to do something like this to create dictionary in order to avoid the above problem.

>>> keys = ['Valid', 'Reverse']
>>> NOC = dict.fromkeys(keys, [])
>>> for key in NOC.keys():
...     NOC[key] = []
...
>>> NOC
{'Valid': [], 'Reverse': []}
>>> id(NOC['Valid'])
50173312
>>> id(NOC['Reverse'])
11515384

Now the append works perfect.

5. Data Structures, Sort the items of the list in place (the arguments can be used for sort combs = [] >>> for x in [1,2,3]: for y in [3,1,4]: if x != y: combs.append((x, y)) . The dict​() constructor builds dictionaries directly from sequences of key-value pairs: >>> Let's pretend you want to live in the immutable world and do NOT want to modify the original but want to create a new dict that is the result of adding a new key to the original. In Python 3.5+ you can do: params = {'a': 1, 'b': 2} new_params = {**params, **{'c': 3}} The Python 2 equivalent is:

If the provided value is a mutable object (whose value can be modified) like list, dictionary, etc., when the mutable object is modified, each element of the sequence also gets updated.

This is because, each element is assigned a reference to the same object (points to the same object in the memory). you must be use dictionary comprehension keys = ['Valid', 'Reverse'] NOC = { key: [] for key in keys }

PEP 584 -- Add Union Operators To dict, Raise; Add The Values (As Counter Does, with +); Leftmost Value (First-Seen) Use The Addition Operator; Use The Left Shift Operator; Use A New Left Arrow Similarly, the iteration order of the key-value pairs in the dictionary will TypeError: can only merge dict (not "list") to dict >>> d |= [('spam', 999)]  It accepts in iterable sequence of key value pairs that can be single key value pair or list of tuples or an another dictionary. For each entry in sequence, it will add the given key value pair in dictionary and if key already exists then it will update its value. Let’s use this to add new key value pairs in dictionary or updating existing one.

Here the problem is that all values point to the same list. An efficient and Pythonic solution would be to use collections.defaultdict.

from collections import defaultdict
NOC = defaultdict(list)

Hope it satisfies your requirements.

Lists, Tuples, Dictionaries, You are probably familiar with syntax like [1, 2, 3, 4] . (It is worth noting that the empty list evaluates to False when used in a boolean context.) concatenate two lists together; get the length of the list; add an item to the list Tuples, being immutable, can be used as a key in a dictionary, which we're about to learn about​. Adding all elements of one list to another using list.extend() It will add all elements of list1 at the end of list. Basically it will merge the two lists i.e. list append() vs extend() list.append(item) , considers the parameter item as an individual object and add that object in the end of list.

Find all occurences of a key in nested python dictionaries and lists , Find all occurences of a key in nested python dictionaries and lists In that case add a check for it and skip the element (added line 2 before last): Python3 users should replace dictionary.iteritems() with dictionary.items() . iteritems was I changed it a bit for my use case, where I needed to get all contained keys of a  I want to add an item to an existing dictionary in python. For example, this is my dictionary: How to achieve this? i cant believe this inline solution hasn't been posted yet. We can use { **default_data, 'item3':3} which returns the updated array. Very useful for lambda functions and list comprehensions. (requires PEP 448 (Python 3.5

Dictionary Manipulation in Python, Output: The dictionary contains the following keys: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Accessing To get a value To delete an element from a dictionary, use del To add a new element to a dictionary, assign a value to a new key months[5] Combining List and Dictionary Example of a list of dictionaries Using dict(**orig) will create a new temporary dict that is used as keyword argument for the dict constructor. The constructor will then copy the values from this argument into itself. So you create an additional dictionary with dict(**orig). This is wasteful when the dictionaries are big, as in the original question.

4. Dictionaries and Sets - High Performance Python [Book], Sets and dictionaries are ideal data structures to be used when your data has no This reference object is called the “key,” while the data is the “value. If it is a new unique name, we add it to our list of unique names. for a dictionary with eight elements, we only look at the last 3 bits since at that point the mask is 0x111 ). Python Dictionary basically contains elements in the form of key-value pairs. When you want to add to dictionary in Python, there are multiple methods that

Comments
  • I love Python because the answers are often so easy. Thank you for helping. I spent two hours on this and never once considered that I was creating a single list because of how I was creating the variables. Thank you, thank you, thank you.
  • I wish I could mark all answers correct - but thank you so much Krushi
  • I wish I could mark all answers correct - but thank you so much Iro