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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:41:31+00:00 2026-06-17T15:41:31+00:00

Possible Duplicate: Return the first item in a list matching a condition How to

  • 0

Possible Duplicate:
Return the first item in a list matching a condition

How to easily grab the “matching” list element using list comprehension? e.g. I’ve a list and I’m looking for a element that starts with a a certain string. That’s easy to do:

>>> lines = ['AHP Buildlife number', 'NIT-Version-Default-v0.16.0', 'E_release v2.3.14.0']
>>> [ x.strip() for x in lines if x.startswith('NIT-Version-Default') ]
['NIT-Version-Default-v0.16.0']

But how can I do the same from in a if statement so that the matching list-element can be used for further processing; some thing like this:

>>> if [ x.strip() for x in lines if x.startswith('NSS-Checkin-Default') ]:
...   ver = x.split('-')[-1].strip()
...   print ver

So, that it return v0.16.0 as the version number. This obviously doesn’t work but hopefully you get the idea what I’m trying to do. Any idea how to do that? Cheers!!

PS. You are welcome to improve the question or the title.

  • 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-17T15:41:32+00:00Added an answer on June 17, 2026 at 3:41 pm

    If you’re looking for only one element:

    x = next(x.strip() for x in lines if x.startswith('NSS-Checkin-Default'))
    ver = x.split('-')[-1].strip()
    

    Note that I’ve used a generator expression (a for b in c if d), instead a list comprehension [a for b in c if d]. This way the iteration over lines will only be done for as long as actually needed (in this case, until the first element is found).

    If no element is found, next will raise StopIteration.


    If you additionally want to make sure that there’s exactly one element (or exactly N elements for some fixed N), you could write it this way (assign to a tuple):

    (x,) = (x.strip() for x in lines if x.startswith('NSS-Checkin-Default')
    

    In that case you’ll have a ValueError if the generator expression will yield too few or too many elements. This is called “tuple unpacking”.

    See also:

    What is the best way to get the first item from an iterable matching a condition?

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

Sidebar

Related Questions

Possible Duplicate: Python: find first element in a sequence that matches a predicate Is
Possible Duplicate: check whether a List<string> contains an element in another List<string> using LINQ
Possible Duplicate: F# equivalent of LINQ Single This function should return the first element
Possible Duplicate: return AJAX callback return I have made a JQuery code using AJAX,
Possible Duplicate: Java: Get first item from a collection In Java, I often encounter
Possible Duplicate: Currying subtraction I started my first haskell project that is not from
Possible Duplicate: Finding first and last index of some value in a list in
Possible Duplicate: trailing return type using decltype with a variadic template function I want
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: Robust, Mature HTML Parser for PHP I'm trying to grab the first

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.