Replace value(s) in sublists, using a dictionary
<p>I have a dictionary, called <code>dictionary</code> and a list of lists (<code>newdetails</code>). I want to look up the key (which is the gtin) in the dictionary and if it matches the <code>index[0]</code> in each sublist in <code>newdetails</code>, to replace <code>index[3]</code> of each sublist with the corresponding value (<code>currentstock</code>) in the dictionary. </p>

<p>The dictionary, on running the program is here:
12121212 below is the <code>gtin</code> and <code>1</code> refers to the current stock. </p>

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

<p>and this is <code>newdetails</code> - in this particular iteration it contains two sublists:</p>

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

<p>And the code I have so far is:</p>

<pre><code>#newdetails is the list of lists
for sub_list in newdetails:       
    sub_list[3] = dictionary.get(gtin), (currentstock)
    print(sub_list)


#print items in the dictionary for reference 
for gtin,currentstock in dictionary.items():
    print(gtin, currentstock)
</code></pre>

<p>As mentioned, I want the output to be:</p>

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

<p>But instead it is:</p>

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

<p>UPDATE based on suggestions. The code is now:</p>

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

        #print(sub_list)
        print("After - ",newdetails)

        #print items in the dictionary for reference 
    for gtin,currentstock in dictionary.items():
        print(gtin, currentstock)
</code></pre>

<p>The output however is:</p>

<pre><code>[['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung  Laptop', '900', '5', '3', '5']]
After -  [['12345670', 'Iphone 9.0', '500', None, '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
After -  [['12345670', 'Iphone 9.0', '500', None, '3', '5'], ['12121212', 'Samsung Laptop', '900', None, '3', '5']]
#Contents of dictionary here
0 0
12345670 2
12121212 4
</code></pre>

<p>In the above example, it should be replacing the 5 in each sublist (that is the index[03] in each sublist, with 2 and 4 respectively for the corresponding gtin. But it doesn't quite do that yet?</p>

<p><em>Thanks <strong>@MartijnPieters</strong> for all your comments.</em> </p>

<p><em>Will post all three versions of my code which I converted from less efficient to efficient with your help.</em></p>

<p><strong>Version 1 :</strong> (<em>Refer to comment no 1 from Martijn</em>)</p>

<pre><code> for k,v in dictionary.iteritems():
    for sub_list in newdetails:
        if k == sub_list[0]:
            sub_list[3] = v
</code></pre>

<p><strong>Version 2:</strong> (<em>Refer to comment no 2 from Martijn</em>)</p>

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

<p><strong>Version 3 (Final and good one) :</strong></p>

<pre><code>dictionary={'0' :0, '12121212': 1,'12345670' :0}

newdetails = [['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung   Laptop', '900', '5', '3', '5']]
print "Before - ",newdetails

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

<p><strong>Output:</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Before -  [['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
After -  [['12345670', 'Iphone 9.0', '500', 0, '3', '5'], ['12121212', 'Samsung Laptop', '900', 1, '3', '5']]

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

python dictionary replace