Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list.index() help
#1
I have a 2D list called sampleArray. type(sampleArray) shows that it is a list. A small sample of the list is:

sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007],
 ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043],
 ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084],
 ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102],
 ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116],
 ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139],
 ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165],
 ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179],
 ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195],
 ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205],
 ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]]
I want to find out the index associated with particular date entry. So I naively tried
sampleArray.index('2020-07-25')
and receive in response: ValueError: '2020-07-25' is not in the list.
But it is - its located at sampleArray[3][0].

Ok, so I tried
sampleArray.index(9679)
and receive the same response: ValueError: 9679 is not in the list.
Yet it is located at sampleArray[9][1]

So I took just one row from sampleArray:
test = sampleArray[8]
test
and have returned:
['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195]
I try
test.index('2020-07-30')
test.index(134)
and get the returns I am expecting: 0 and 2.

So there is something about a 2D list and .index() that I haven't figured out yet. Can anyone offer some insight as to what I am doing wrong?

TNX
Reply
#2
sampleArray is 2D - i.e. list of lists. list.index() method does not work on multiple levels deep. from the docs:
Quote: list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item

spam = [[1, 2], [3, 4]]
eggs = [3, 4]
print(spam.index(eggs))
Output:
1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
list.index() is for finding an element in a 1D list. It won't descend into sub-lists that represent multi-dimensional arrays.

If you want to roll your own, you'll have to try to look for the data in each row separately. Something like:

sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007],
 ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043],
 ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084],
 ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102],
 ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116],
 ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139],
 ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165],
 ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179],
 ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195],
 ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205],
 ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]]

find_it = '2020-07-25'

for row_index, row in enumerate(sampleArray):
    if find_it in row:
        print(f"[{row_index}][{row.index(find_it)}]")
Output:
[3][0]
Otherwise, you could convert this to a numpy array and use numpy index.

import numpy as np

sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007],
 ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043],
 ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084],
 ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102],
 ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116],
 ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139],
 ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165],
 ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179],
 ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195],
 ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205],
 ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]]

find_it = '2020-07-25'
arr = np.array(sampleArray)
print(np.where(arr == find_it))
Output:
(array([3]), array([0]))
Reply
#4
Index only works on 1D lists because that is all there is in Python, You can make something that looks like a 2D list but it is really just a list of lists. That is why our index() call failed. Your list "sampleArray" only contains lists. None of these lists is '2020-07-25' or 9679, so index() correctly raises a ValueError.

bowlofred's numpy example works because numpy understands lists that contain lists that contain lists and knows how to do a deep search.
Reply
#5
buren, bowlofred, deanhystad

Thanks for the info. I like the idea of using numpy so will go with that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 1,844 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,409 May-03-2022, 01:39 PM
Last Post: Anldra12
  IndexError: list index out of range rf_kartal 6 2,761 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,348 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  Changing Index of 2 List in python giddyhead 0 1,640 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  IndexError: list index out of range ramu4651 2 3,693 Jan-24-2021, 01:45 PM
Last Post: buran
Question Matching variable to a list index Gilush 17 5,702 Nov-30-2020, 01:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020