How do you get at a value in a list of lists /replace it with a dictionary value?
<p>I need to get at a value in a list of lists. The list in question is called 'newdetails'. It's contents are below</p>

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

<p>I found that print(newdetails[0]) prints the entire first list. What I need however, is to get at index[0] of the first list which is 12345670. I also need to replace index 03 (of both lists) with the VALUE in a dictionary, which corresponds to the first GTIN number.</p>

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

<pre><code>for gtin,currentstock in dictionary.items():
        if newdetails[0]==gtin:
            newdetails[3]=currentstock
    print("checking to see what newdetails[0] and [3] and [9] is")
    print(newdetails[0])
    print("or is it a matrix or a 2d array")
    print(newdetails[0,3])
    print("now will this work...print replacement list")
    print(newdetails)
</code></pre>

<p>Can someone help?</p>

<p>UPDATE:</p>

<p>Thank you for your suggestion. 
I tried this: (but it came up with an error)</p>

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

<p>Error:
        sub_list[3] = dictionary(sub_list[3], sub_list[3])
        TypeError: 'dict' object is not callable</p>

<p>To clarify, the list i have is called 'newdetails' - and it has two lists inside it (read in from a file).
The name of the dictionary is simply dictionary (for now), and it has a GTIN key and a 'currentstock' VALUE. I want the GTIN key in the dictionary that corresponds with the same GTIN value in BOTH lists to update index 3 (currently showing as 5),. with the value 'currentstock' in the dictionary, that corresponds to the GTIN number.</p>

<p>Thanks in advance to the helpful genius who can help me solve this!</p>

<p>Let's say you are having a list of <code>key</code>s mapped to corresponding element in the sub-list mentioned by you in the question. Below is the sample code to achieve that:</p>

<pre><code>my_list = [['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
my_key = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
dict_list = [{k: v for k, v in zip(my_key, sub_list)} for sub_list in my_list]

# Value of "dict_list":
[{
      'item2': 'Iphone 9.0', 
      'item3': '500', 
      'item6': '5', 
      'item4': '5', 
      'item5': '3', 
      'item1': '12345670'
 }, 
 {
      'item2': 'Samsung Laptop', 
      'item3': '900', 
      'item6': '5', 
      'item4': '5',
      'item5': '3', 
      'item1': '12121212'
 }]
</code></pre>

<p>To know more about how this code worked, check <a href="https://docs.python.org/2/library/functions.html#zip" rel="nofollow"><code>zip()</code></a> and <a href="http://stackoverflow.com/questions/14507591/python-dictionary-comprehension"><code>Dict Comprehension in Python</code></a>.</p>

<p>In case you want <strong>to update the <code>value</code> of column</strong>, let's say 'item6' of <code>0</code>th index in the list, you may do:</p>

<pre><code>dict_list[0]['item6'] = 'new value'
</code></pre>

<p><strong>Update</strong>: Based on the comment fromm the OP</p>

<pre><code># my_gtin &lt;-- GTIN dict
for sub_list in my_list:
    sub_list[3] = my_gtin.get(sub_list[3], sub_list[3])
</code></pre>

<p>Above code will update the entry of <code>3</code>rd index in <code>sub_list</code> based on if <code>key</code> is present in <code>my_gtin</code> dict. Else <code>sub_list</code> will have the same value.</p>

python list dictionary