Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find index value in List
#1
hi,

is there any good ways to find the location of a value in a list that consist of multiple brackets?

so for example:
test = ["54", "cat", "99", "1238"], ["758", "tre", "1233124"], ["hva skjer", "15684", "0"]
print(test.index("758")
gives the error:
print(test.index("cat"))
ValueError: tuple.index(x): x not in tuple
[Finished in 0.2s with exit code 1]
Reply
#2
>>> [item.index('cat') for item in test if 'cat' in item]
[1]
>>> [item.index('758') for item in test if '758' in item]
[0]
Reply
#3
The method index raises a ValueError, if the value does not exist in the list.
In addition, you have a 2d list, where you can't just use the method index.

Before you use the method index, you could look up with the in operator, if the value is in the list.

my_list = [1, 2, 3]
# code
if 2 in my_list:
    print("Index:", my_list.index(2))
But you have a 2d-list. You can nest for-loops, but it's not required in this case.
Loop over the rows and you get the columns.
For each column you look if the value you seek is in the row.
If this is the case, just return the row_idx and col_idx.


test = ["54", "cat", "99", "1238"], ["758", "tre", "1233124"], ["hva skjer", "15684", "0"]


def index2d(matrix, value):
    for row_idx, row in enumerate(matrix):
        if value in row:
            return row_idx, row.index(value)


index2d(test, "99")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
What is your expected return value for test.index("758")? I'm sure you can write a function that returns the desired value, but what is the desired value in this case? 1? 4? (1, 0)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program to find Mode of a list PythonBoy 6 1,000 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,049 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,157 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,372 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  IndexError: list index out of range dolac 4 1,847 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,907 May-31-2022, 01:58 PM
Last Post: Larz60+
  IndexError: list index out of range Anldra12 2 1,411 May-03-2022, 01:39 PM
Last Post: Anldra12
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,819 Jan-23-2022, 07:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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