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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:43:42+00:00 2026-06-15T04:43:42+00:00

How do I look up the ‘id’ associated with the a person’s ‘name’ when

  • 0

How do I look up the ‘id’ associated with the a person’s ‘name’ when the 2 are in a dictionary?

user = 'PersonA'
id = ? #How do I retrieve the 'id' from the user_stream json variable?

json, stored in a variable named “user_stream”

[
  {
    'name': 'PersonA',
    'id': '135963'
  },
  {
    'name': 'PersonB',
    'id': '152265'
  },
]
  • 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-15T04:43:44+00:00Added an answer on June 15, 2026 at 4:43 am

    You’ll have to decode the JSON structure and loop through all the dictionaries until you find a match:

    for person in json.loads(user_stream):
        if person['name'] == user:
            id = person['id']
            break
    else:
        # The else branch is only ever reached if no match was found
        raise ValueError('No such person')
    

    If you need to make multiple lookups, you probably want to transform this structure to a dict to ease lookups:

    name_to_id = {p['name']: p['id'] for p in json.loads(user_stream)}
    

    then look up the id directly:

    id = name_to_id.get(name)  # if name is not found, id will be None
    

    The above example assumes that names are unique, if they are not, use:

    from collections import defaultdict
    
    name_to_id = defaultdict(list)
    for person in json.loads(user_stream):
        name_to_id[person['name']).append(person['id'])
    
    # lookup
    ids = name_to_id.get(name, [])  # list of ids, defaults to empty
    

    This is as always a trade-off, you trade memory for speed.

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

Sidebar

Related Questions

Look to identify the user, email, or phone identifier from Android 1.5. I've seen
Look at this example code, from a Telerik MVC grid: <% Html.Telerik().Grid(Model.InstallerList) .Name(InstallerGrid) .DataKeys(key
Look the code below. I'd like to replace USERNAME by the field name received
Look this xsl template: <xsl:template match=root> <xsl:param name=bla select=/.. /> <ha> <xsl:value-of select=$bla />
Look at the following example (partially taken from MSDN Blog ): class Animal {
Look at the line THIS LINE #### in following example. <ListBox Grid.Row=0 x:Name=listBoxServers> <ListBoxItem
Look at onComplete results: currently item.name is always rty , but should be (1/2):
Look at the picture below. I'm loading an image from sdcard with code belove.
Look at the following method: -(void)updateProfile:(Profile *)profile WithJSON:(NSString *)JSON; { SBJSON *parser = [[SBJSON
Look at the following code from the ThreadFactory interface public Thread<T> newThread(Runnable<T> runnable); What

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.