Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:54:28+00:00 2026-05-28T02:54:28+00:00

I am using Python and I would like to check the location of a

  • 0

I am using Python and I would like to check the location of a value in a list of lists against other indexes.

Like if I had 1 at (1, 1) I would want to be able to check if 1 were at the other indexes so I could make something happen based on which index it matched.

For example:

list_of_lists = [
    [4, 5, 6],
    [7, 1, 8],
    [6, 2, 9]
    ]
if 1 in row for row in list_of_lists:
    if index of 1 is (0, 0), (0, 1), or (0, 2)
        print ("It is in the First row!")
    if index of 1 is (1, 0), (1, 1), or (1, 2)
        print ("It is in the second row!")

If this worked correctly it should print: “It is in the second row!” Because the index of 1 matches with one of the indices in the third if statement. They may not necessarily be rows in some instances where I would be using this. So If you could provide a way that would not use the rows in your answer. Just a way to look at indexes and compare them. Obviously this is not the correct syntax. But how would I do that in Python? How would I get the index of 1 and compare it to other indexes in a list of lists?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:54:28+00:00Added an answer on May 28, 2026 at 2:54 am

    trying this in a python file:

    list_of_lists = [
        [4, 5, 6],
        [7, 0, 8],
        [6, 2, 9]
    ]
    
    def index_of(value, matrix):
        for i, r in enumerate(matrix):
            for j, c in enumerate(r):
                if c == value:
                    # returns the first instance
                    return (i,j)
        return None
    
    if index_of(0, list_of_lists) == (1,1):
        print "hey"
    
    list_of_lists2 = [
        [0, 5, 6],
        [7, 0, 8],
        [0, 2, 9]
    ]
    
    def indexes_of(value, matrix):
        return [(i,j) for i, r in enumerate(matrix) for j, c in enumerate(r) if c == value]
    
    print indexes_of(0, list_of_lists2)
    

    outputs

    hey
    [(0, 0), (1, 1), (2, 0)]
    

    [EDIT] As requested:

    First of all, what does enumerate do?

    >>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    >>> list(enumerate(seasons))
    [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    >>> list(enumerate(seasons, start=1))
    [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
    

    source

    As you can see, if you use enumerate, it will return both the index of the element AND the element. Therefore, while you’re iterating over the returned list you can access them and do what you desire. That’s what I’m doing here:

    for i, r in enumerate(matrix)
    

    In that case, i stands for the row index, and r stands for row itself. ok?

    In the next line you do the same thing, but now you’re enumerating the row itself, ok? And you’re getting, now, the indexes of the values j and the values c that are stored in each row.

    Finally, when you return (i,j), you’re just returning a tuple that represents the values’s matrix index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python's Imaging Library I want to create a PNG file. I would like
I am using python and would like a simple api or regex to check
Using Python I would like to find the date object for last Wednesday. I
I am interested in making a Google Talk client using Python and would like
I am working on a web application using Python (Django) and would like to
I'm using python Textile to store markup in the database. I would like to
I've produced a python egg using setuptools and would like to access it's metadata
I would like to run a process from Python (2.4/2.5/2.6) using Popen , and
Working with deeply nested python dicts, I would like to be able to assign
Using python's optparse module I would like to add extra example lines below the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.