Replacing a specific index value in list of lists with corresponding dictionary value
<p>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. </p>

<p>The content/details of the dictionary is as follows:</p>

<pre><code>dictionary = dict(zip(gtincodes, currentstockvalues))
</code></pre>

<p>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. </p>

<p>I now need to look up a list <strong>NEWDETAILS</strong> which has been read in from file. The list is essentially a list of lists. When newdetails (the list) is printed the output is:</p>

<pre><code>[['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
</code></pre>

<p><strong>WHAT NEEDS TO BE DONE:</strong></p>

<p>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. </p>

<p>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. </p>

<p>The code I have so far is: </p>

<pre><code>for sub_list in newdetails:
        sub_list[3] = dictionary.get(sub_list[3], sub_list[3])
        print(sub_list)
</code></pre>

<p>The above simply appears to produce two seperate sublists and print them. It is not making the replacement.</p>

<pre><code>['12345670', 'Iphone 9.0', '500', '5', '3', '5']
['12121212', 'Samsung Laptop', '900', '5', '3', '5']
</code></pre>

<p>My question is: </p>

<p>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?</p>

<p>Thanks in advance. </p>

<p><strong>UPDATE</strong></p>

<p>Based on Alex P's suggestion (thank you) I made the following edit:</p>

<pre><code> for sub_list in newdetails:
        sub_list[3] = dictionary.get(gtin), sub_list[3]
        print(sub_list)
</code></pre>

<p>It comes up with a replacement (of a tuple) rather than just the individual value - as follows: </p>

<pre><code>['12345670', 'Iphone 9.0', '500', (1, '5'), '3', '5']
['12121212', 'Samsung Laptop', '900', (1, '5'), '3', '5']
</code></pre>

<p>The contents of the dictionary:</p>

<pre><code>12121212 3
0 0
12345670 1
</code></pre>

<p>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. </p>

<p><strong>UPDATE based on suggestion by Moses K</strong></p>

<p>I tried this - thank you - but ....</p>

<pre><code>for sub_list in newdetails:
        sub_list[3] = dictionary.get(int(sub_list[0]), sub_list[3])
        print(sub_list)
</code></pre>

<p>the output is still simply the two (unchanged) sublists. </p>

<pre><code>['12345670', 'Iphone 9.0', '500', '5', '3', '5']
['12121212', 'Samsung Laptop', '900', '5', '3', '5']
</code></pre>

<p>Update #2 - both converted to int. </p>

<pre><code>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)
</code></pre>

<p>Still producing an output of:</p>

<pre><code>['12345670', 'Iphone 9.0', '500', 5, '3', '5']
['12121212', 'Samsung Laptop', '900', 5, '3', '5']
</code></pre>

<p>instead of (what I want)that is:
['12345670', 'Iphone 9.0', '500',<strong>2</strong>, '3', '5']
['12121212', 'Samsung Laptop', '900', <strong>1</strong>, '3', '5']</p>

<p>Your <code>GTIN</code> code at <em>index 0</em> of each sublist should be your dictionary key:</p>

<pre><code>for sub_list in newdetails:
      sub_list[3] = dictionary.get(sub_list[0], sub_list[3])
#                                           ^
</code></pre>

<p>If the codes in the dictionary are integers and not strings then you'll need to cast them to <code>int</code>:</p>

<pre><code>for sub_list in newdetails:
      sub_list[3] = dictionary.get(int(sub_list[0]), sub_list[3])
#                                           ^
</code></pre>

python dictionary sublist