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


Messages In This Thread
list.index() help - by qmfoam - Aug-06-2020, 07:53 PM
RE: list.index() help - by buran - Aug-06-2020, 08:05 PM
RE: list.index() help - by bowlofred - Aug-06-2020, 08:14 PM
RE: list.index() help - by deanhystad - Aug-06-2020, 10:02 PM
RE: list.index() help - by qmfoam - Aug-07-2020, 07:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,357 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 1,929 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,450 May-03-2022, 01:39 PM
Last Post: Anldra12
  IndexError: list index out of range rf_kartal 6 2,864 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,326 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,233 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,580 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  Changing Index of 2 List in python giddyhead 0 1,688 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  IndexError: list index out of range ramu4651 2 3,759 Jan-24-2021, 01:45 PM
Last Post: buran
Question Matching variable to a list index Gilush 17 5,930 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