Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using .index(" ") for nested list in python

I want to get the index of the "4" in the nested list, it is working cause I suppress the error. any idea for a alternative?

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in range(len(a)):
    try:
        index = a[i].index(4)
        print(i, index)  
    except:
        pass

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is code that won’t throw an error and doesn’t use index() function.

for i in range(len(a)):
    for j in range(len(a[i])):
        if a[i][j] == 4:
            print("Found at", i, j)
            break
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading