Reputation:
I am trying to replace a value in a list of lists (in all relevant sublists) with a VALUE from a dictionary and cannot quite get it to work.
The content/details of the dictionary is as follows:
dictionary = dict(zip(gtincodes, currentstockvalues))
The dictionary contains pairs of GTIN codes and currentstock values. So, 12121212(GTIN)corresponding to value (currentstockvalue) 1, and 12345670 corresponding to value (currentstockvalue) 0.
I now need to look up a list NEWDETAILS which has been read in from file. The list is essentially a list of lists. When newdetails (the list) is printed the output is:
[['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
WHAT NEEDS TO BE DONE:
I would like to use the dictionary to update and replace values in the list (and all relevant sublists in the list). So, for each GTIN (key) in the dictionary, the 3rd index in each sublist (for the corresponding GTIN) needs to be updated with the VALUE (currentstockvalue) in the dictionary.
In the above example, for sublist 1-index[03] of the sublist (whcih is currently 5) needs to be updated with say 2...(or whatever is the value in the dictionary for that GTIN). The same needs to happen for the second sublist.
The code I have so far is:
for sub_list in newdetails:
sub_list[3] = dictionary.get(sub_list[3], sub_list[3])
print(sub_list)
The above simply appears to produce two seperate sublists and print them. It is not making the replacement.
['12345670', 'Iphone 9.0', '500', '5', '3', '5']
['12121212', 'Samsung Laptop', '900', '5', '3', '5']
My question is:
How do I amend the above code to LOOK UP THE DICTIONARY (to match the index[0]) of each sublist, and REPLACE the 4th element(index[03]) of each sublist with the VALUE in the dictionary for the corresponding GTIN?
Thanks in advance.
UPDATE
Based on Alex P's suggestion (thank you) I made the following edit:
for sub_list in newdetails:
sub_list[3] = dictionary.get(gtin), sub_list[3]
print(sub_list)
It comes up with a replacement (of a tuple) rather than just the individual value - as follows:
['12345670', 'Iphone 9.0', '500', (1, '5'), '3', '5']
['12121212', 'Samsung Laptop', '900', (1, '5'), '3', '5']
The contents of the dictionary:
12121212 3
0 0
12345670 1
12121212 is the gtin and '3' is the current stock. I want to look up the dictionary, IF a corresponding GTIN is found in the newdetails list, I want to replace the third index (fourth element) in the newdetails list, with the corresponding value in the dictionary. So - for 12121212, 5 to be replaced with 3. And for 12345670, 5 to be replaced with 1.
UPDATE based on suggestion by Moses K
I tried this - thank you - but ....
for sub_list in newdetails:
sub_list[3] = dictionary.get(int(sub_list[0]), sub_list[3])
print(sub_list)
the output is still simply the two (unchanged) sublists.
['12345670', 'Iphone 9.0', '500', '5', '3', '5']
['12121212', 'Samsung Laptop', '900', '5', '3', '5']
Update #2 - both converted to int.
for sub_list in newdetails:
print("sublist3")
print(sub_list[3])
sub_list[3] = dictionary.get(int(sub_list[0]), (int(sub_list[3])))
print(sub_list)
Still producing an output of:
['12345670', 'Iphone 9.0', '500', 5, '3', '5']
['12121212', 'Samsung Laptop', '900', 5, '3', '5']
instead of (what I want)that is: ['12345670', 'Iphone 9.0', '500',2, '3', '5'] ['12121212', 'Samsung Laptop', '900', 1, '3', '5']
Upvotes: 1
Views: 307
Reputation: 78554
Your GTIN
code at index 0 of each sublist should be your dictionary key:
for sub_list in newdetails:
sub_list[3] = dictionary.get(sub_list[0], sub_list[3])
# ^
If the codes in the dictionary are integers and not strings then you'll need to cast them to int
:
for sub_list in newdetails:
sub_list[3] = dictionary.get(int(sub_list[0]), sub_list[3])
# ^
Upvotes: 1