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 8537731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:58:12+00:00 2026-06-11T10:58:12+00:00

I have a 2D list where each row has an index, name, and a

  • 0

I have a 2D list where each “row” has an index, name, and a path like [(1L, "bar", "foo/bar"), (2L, "app", "some/app"),] etc. I am trying to retrieve a “row” from this 2D list given and index. For example, an index of 1 should return (1L, "bar", "foo/bar"). I know I can loop through my whole list and compare the index until I find the object like so:

my_index = 1    
for row in my_list:
    if (row[0] == my_index)
        return row
return False

I was wondering if there is a cleaner/nicer way to do this in python, since I am new to python. I know there is an index method for a list that returns the index from a list but I’m not sure how I can use that with a 2D list. Thanks in advance! Oh and also, it can be assumed that there will only be one instance of each object (i.e: no duplicates)

  • 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-06-11T10:58:13+00:00Added an answer on June 11, 2026 at 10:58 am

    It looks like you would be better off using a dictionary instead of a list, so your data would look like this:

    my_dict = {1L: (1L, "bar", "foo/bar"),
               2L: (2L, "app", "some/app")}
    

    This allows you to efficiently access each element by index:

    >>> my_dict[1]
    (1L, 'bar', 'foo/bar')
    

    Here is how you could create the dictionary from the list:

    my_dict = {row[0]: row for row in my_list}
    

    Or on Python 2.6 and below:

    my_dict = dict((row[0], row) for row in my_list)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom list view, each row has it's own custom selector,
Currently I have a list view within it each row has a checkbox attached.
I have a list of roughly 3000 rows. Each row has a different end
I have a Todo List (an ASP.MVC UserControl) that for each row has the
I have a list view with 10 columns and I want each row to
I have a list. Each row includes an Image at left and two text
I have a list, each row consisting of a main item and subitem. I
Let's say I have a list of primary keys, for each row one value
I have a table with multiple rows in each row is a select list
I have a list of items. When I create the list each item has

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.