Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding item to Dictionary within loop

Below data is grasped from webpage and containing entries as below(like a table with many rows):

entry1: key1: value1-1, key2: value2-1, key3: value3-1
entry2: key1: value1-2, key2: value2-2, key3: value3-2
entry3: key1: value3-1, key2: value2-3, key3: value3-3
......
entry100: key1: value100-1, key2: value100-2, key3: value100-3

how can I use a dictionary to store this data? the data is from a list thus the 'dictionary append' should be done within a loop...

here is my current solution:

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list.update(case)

but the case_list in the end only contains the last case entry... Can somebody please help me on this? I would expect the case_list containing 100 entries w/o any overwriting among entries, and I will need to store it to DB afterwards.

like image 493
Kay Avatar asked Jul 02 '15 10:07

Kay


People also ask

How do I add an item to a dictionary in dictionary?

Method 1: Using += sign on a key with an empty value In this method, we will use the += operator to append a list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.

How do I add an item to a dictionary in Python?

There is no add() , append() , or insert() method you can use to add an item to a dictionary in Python. Instead, you add an item to a dictionary by inserting a new index key into the dictionary, then assigning it a particular value.

Can you append things to a dictionary?

Appending element(s) to a dictionaryTo append an element to an existing dictionary, you have to use the dictionary name followed by square brackets with the key name and assign a value to it.


1 Answers

In your current code, what Dictionary.update() does is that it updates (update means the value is overwritten from the value for same key in passed in dictionary) the keys in current dictionary with the values from the dictionary passed in as the parameter to it (adding any new key:value pairs if existing) . A single flat dictionary does not satisfy your requirement , you either need a list of dictionaries or a dictionary with nested dictionaries.

If you want a list of dictionaries (where each element in the list would be a diciotnary of a entry) then you can make case_list as a list and then append case to it (instead of update) .

Example -

case_list = []
for entry in entries_list:
    case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
    case_list.append(case)

Or you can also have a dictionary of dictionaries with the key of each element in the dictionary being entry1 or entry2 , etc and the value being the corresponding dictionary for that entry.

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list[entryname] = case  #you will need to come up with the logic to get the entryname.
like image 51
Anand S Kumar Avatar answered Oct 08 '22 05:10

Anand S Kumar