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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:16:35+00:00 2026-05-24T09:16:35+00:00

lst = [{id: 1, name: ernest}, …. {id: 16, name:gellner}] for d in lst:

  • 0
lst = [{"id": 1, "name": "ernest"}, .... {"id": 16, name:"gellner"}]

for d in lst:
    if "id" in d and d["id"] == 16:
        return d

I want to extract the dictionary that the key “id” equals to “16” from the list.

Is there a more pythonic way of this?

Thanks in advance

  • 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-24T09:16:37+00:00Added an answer on May 24, 2026 at 9:16 am

    Riffing on existing answers to get out of the comments:

    Consider using a generator expression when you only need the first matching element out of a sequence:

    d16 = (d for d in lst if d.get('id') == 16).next()
    

    This is a pattern you’ll see in my code often. This will raise a StopIteration if it turns out there weren’t any items in lst that matched the condition. When that’s expected to happen, you can either catch the exception:

    try:
        d16 = (d for d in lst if d.get('id') == 16).next()
    except StopIteration:
        d16 = None
    

    Or better yet, just unroll the whole thing into a for-loop that stops

    for d16 in lst:
        if d16.get('id') == 16:
            break
    else:
        d16 = None
    

    (the else: clause only gets run if the for loop exhausts its input, but gets skipped if the for loop ends because break was used)

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

Sidebar

Related Questions

I built a dictionary list like this lst = [{'name': nameobj, Classobj1: string, Classobj2:
I have the following code: class Employee { friend string FindAddr( list<Employee> lst,string name
I just want to know the difference between List<string> lst = new List() and
SELECT id,name,SUM(`lsa`) AS lst FROM table_a AS sn, table_b AS fb WHERE sn.uid=fb.uid AND
I have a dictionary, dct = {'slab1': {'name':'myn1', 'age':20}, 'slab2':{'name':'myn2','age':200}} lst = {'/store/dir1/dir_slab1/tindy', '/store/dir2/dirslab2_fine/tunka','/store/dir1/dirslab3/lunku'}
I am stuck with the following data. There is a list. [{name: '/', children:
I have a fairly long list in python which I want to return parts
I have the following config file for solr: <requestHandler name=/update/extract startup=lazy class=solr.extraction.ExtractingRequestHandler > <lst
I have a list of lists: lst = [[567, 345, 234], [253, 465, 756,
Why is enumerate slower than xrange + lst[i]? >>> from timeit import Timer >>>

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.