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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:43:50+00:00 2026-06-10T01:43:50+00:00

I am new to Python and I did my search but I could not

  • 0

I am new to Python and I did my search but I could not find what I am looking for. I apologise in advance if this question has been asked and if I could not find it due to my lack of not knowing the name of what I am trying to achieve. I will gladly read any document you might suggest.

I have a list of lists. e.g. => [int, ‘str’]

t = [[0234, 'str_0'],
     [1267, 'str_1'],
     [2445, 'str_2']]

I want to find out if a str exists in index(1) position of one of the lists of list t.
I can do this with a function containing 2 for or while loops but what I am seeking is to achieve this, if possible, using one single iteration. I want to learn the shortest function.

for input str('str_3'), I want to get int(2) (index of the list which has str_3 on its own 1st index location)
for str_1, I want to get int(0)

and for str_1234 I want to get False as it is not in any of the lists within the list t

As a newbie, I would normally do:

for n in range(len(t)):
    if t[n][1] == 'str_1'
        return n
    return False

What I am seeking to get is, if possible, a better and shorter way of achieving this in one line of a code or just simply to learn if there is a smarter, better or more pythonic way that any one of you who is surely more experienced would recommend.

Thank you

  • 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-10T01:43:51+00:00Added an answer on June 10, 2026 at 1:43 am
    [n for n, (i, s) in enumerate(t) if s == 'str_3']
    

    Explanation:

    >>> t = [[100, 'str_1'], [200, 'str_2'], [300, 'str_3']]
    
    # Use enumerate to get each list item along with its index.
    >>> list(enumerate(t))
    [(0, [100, 'str_1']), (1, [200, 'str_2']), (2, [300, 'str_3'])]
    
    # Use list comprehension syntax to iterate over the enumeration.
    >>> [n for n, (i, s) in enumerate(t)]
    [0, 1, 2]
    
    # An if condition can be added right inside the list comprehension.
    >>> [n for n, (i, s) in enumerate(t) if s == 'str_3']
    [2]
    >>> [n for n, (i, s) in enumerate(t) if s == 'str_1234']
    []
    

    This will return all of the matching indices, since there could be more than one.

    If you know there will only be one index, may I suggest using a dict instead of a nested list? With a dict you can lookup elements in constant time using very straightforward syntax, rather than having to iterate.

    >>> t = {'str_1': 100, 'str_2': 200, 'str_3': 300}
    
    >>> 'str_3' in t
    True
    >>> t['str_3']
    300
    
    >>> 'str_1234' in t
    False
    >>> t['str_1234']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'str_1234'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Squeak and Smalltalk but not programming in general(i've some Python experience).
This is my first question that I haven't been able to find an answer
Well, I hope this is not a duplicate, the search did not yield anything
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
Very new to python and can't understand why this isn't working. I have a
Brand new to Python (and programming in general), if this is simple and/or answered
I am new to Python and have been studying its fundementals for 3 months
I am new to Python and I have written this simple script: #!/usr/bin/python3 import
I am new to C++/Python mixed language programming and do not have much idea
Pretty new to python/programming in general, this is my biggest project yet. I am

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.