How to find index of a given value in a nested list in Python?

I wat to traverse over the nested list below, find 1 then print index of that list.

my_list = [['Hello', 'World','!'],[1, 3, 'Test', 5, 7],[2, 4]]

for item in range(len(my_list)):
    for item2 in range(len(my_list[item])):
        output = my_list[item][item2]
        if output == 1:
            print('the index of 1 is [%d][%d] ' % (item.item2))

The loops above returned AttributeError: 'int' object has no attribute 'item2'

Can anyone tell me how to fix it? Thank you for the help!

Everything seems fine in your code, just make the string formatter a tuple. Modify the last line of your code to this below:

print('the index of 1 is [%d][%d] ' % (item,item2))

Python, In Python, we have several ways to perform the indexing in list, but sometimes, we have more than just an Let's discuss certain ways in which this can be performed. print ( "Index of nested element is : " + str (res)) in custom sliced List � Python | Find the sublist with maximum value in given nested list� 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. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list.

I have used enumerate on the nested list to use both the index of the item in the list and the respective list item.

To find the index of an item use the list_name.index(value) method.

Use in to check the membership i.e. to check if a value is in the list.

my_list = [['Hello', 'World','!'],[1, 3, 'Test', 5, 7],[2, 4]]

for i, item in enumerate(my_list):
    if 1 in item:
        print('Index of 1 is [{}][{}]'.format(i,item.index(1)))

Output:

Python Nested List, You can access individual items in a nested list using multiple indexes. change the value of a specific item in a nested list by referring to its index number. You can use the built-in len() function to find how many items a nested sublist has. You can change the value of a specific item in a nested list by referring to its index number. L = ['a', ['bb', 'cc'], 'd'] L = 0 print(L) # Prints ['a', ['bb', 0], 'd'] Add items to a Nested list To add new values to the end of the nested list, use append () method.

for i in my_list:
    if 1 in i:
        print(str(i.index(1)) + "th element of " + str(i))

This outputs

0th element of [1, 3, 'Test', 5, 7]

Python: Find index of element in List (First, last or all occurrences , Apart from this, we will also discuss a way to find indexes of items in list that satisfy a certain condition. Python: Get index of item in List. To find� Python: Find index of item in list using for loop. Instead of using list.index () function, we can iterate over the list elements by index positions, to find the index of element in list i.e. list_of_elems = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test', 'Ok'] elem = 'is'. pos = -1.

11. Lists — How to Think Like a Computer Scientist: Learning with , We have already seen that we can assign list values to variables or pass lists as It is common to use a loop variable as a list index. a list can contain another list, the nested list still counts as a single element in Similarly, the * operator repeats a list a given number of times: If we output the element at index 3, we get:. Return Value from List index () The index () method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised. Note: The index () method only returns the first occurrence of the matching element.

Python List, Python List: Insert, modify, remove, slice, sort, search element(s) and the list; Insert an item at a given position; Modify an element by using Nested lists in Python; How can I get the index of an element contained in the list? Setting a value in a nested python dictionary given a list of indices and value potentially nested, given a list of indices and a value. Finding the index of

finding the index of elements in nested list, find index of sublist in list python find all index of element in list python. I am trying to create a program that will find and store the index of each element in the nested list. Let's use this function to find the indexes of a given item in the list,� Python: Check if a list or list of lists is empty or not; Python : Count elements in a list that satisfy certain conditions; Python : 3 ways to check if there are duplicates in a List; Python: Find duplicates in a list with frequency count & index positions; Python : Convert list of lists or nested list to flat list

Comments
  • Instead of (item.item2) do (item, item2)
  • Not a functional problem, but I think i1, i2 would be a less confusing naming choice for the loop variables