1

I need to get at a value in a list of lists. The list in question is called 'newdetails'. It's contents are below

[['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]

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.

the code I have so far is:

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)

Can someone help?

UPDATE:

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

for sub_list in newdetails:
        sub_list[3] = dictionary(sub_list[3], sub_list[3])
    print("sublist")
    print(sub_list)

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

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.

Thanks in advance to the helpful genius who can help me solve this!

  • If `newdetails[0]` returns a list how do you access a list? You know this answer just access it the way you did with `newdetails`. `newdetails[0][0]` is basically saying `inner_list = newdetails[0]` and then `inner_list[0]` – MooingRawr Sep 21 '16 at 16:03

3 Answers3

0

If you have a list of lists

data = [['12345670', 'Iphone 9.0', '500', '5', '3', '5'], 
        ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]

Then as you've found data[0] will return the first list in the list. And then if you want the first item again then data[0][0] is what you need.

To replace the third index of both can be done like this:

for row in data:
    row[3] = 'foo'
Chris Charles
  • 4,406
  • 17
  • 31
0

For multidimensional arrays use numpy library.

In python, simply use nested lists indexing:

>>> x = [['12345670', 'Iphone 9.0', '500', '5', '3', '5'], ['12121212', 'Samsung Laptop', '900', '5', '3', '5']]
>>> x[0]
['12345670', 'Iphone 9.0', '500', '5', '3', '5']
>>> x[0][0]
'12345670'
>>> x[0][3] = '600'
>>> x[0][3]
'600'
Uriel
  • 15,579
  • 6
  • 25
  • 46
0

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

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'
 }]

To know more about how this code worked, check zip() and Dict Comprehension in Python.

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

dict_list[0]['item6'] = 'new value'

Update: Based on the comment fromm the OP

# my_gtin <-- GTIN dict
for sub_list in my_list:
    sub_list[3] = my_gtin.get(sub_list[3], sub_list[3])

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

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Hello - I'm about to try your method, but can't see how the code replaces the value in the list. An example of what I want is: In list 1, if item1 (in the dictionary) ==item1 in the list, I want the first 5(that is index 3) to be replaced with item2 in the dictionary –  Sep 21 '16 at 16:16
  • This is just to convert nested list to list of dictionary. Actually I didn't got the part to update the list. If you can explain a bit on how you want to update list with some example, I might be able to help you – Moinuddin Quadri Sep 21 '16 at 16:18
  • Updated the answer at the last based on my understanding. Or do you want to update the particular item of entire list? – Moinuddin Quadri Sep 21 '16 at 16:22
  • Thanks. I'm just about to try it. Basically, I have a dictionary that stores GTIN numbers and corresponding 'CurrentStock' values. If the GTIN number in the dictionary matches the GTIN in the list of lists, then I want the corresponding currentstock value in the dictionary, to replace, the 'currentstock' value in the listoflists (in this case it is the 4 element, so index 3 in each list). –  Sep 21 '16 at 16:57
  • You should have mentioned this in your question. I will update my answer based on this information. And, you should make an edit in the question regarding the same – Moinuddin Quadri Sep 21 '16 at 17:00
  • Update the answer. Check at the last – Moinuddin Quadri Sep 21 '16 at 17:06
  • Thank you so much for your help. I did try something - see update in original - but it still came up with an error. –  Sep 21 '16 at 17:49
  • I am extremely sorry, my bad. I forgot to write `.get`. It is `my_gtin.get(sub_list[3], sub_list[3])`. Updated the answer. check now – Moinuddin Quadri Sep 21 '16 at 18:09
  • I'm afraid it still doesn't work. Update has been posted. –  Sep 22 '16 at 09:28
  • Please check my last comment. Have you tried with `.get()` ? – Moinuddin Quadri Sep 22 '16 at 09:30