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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:37:20+00:00 2026-05-31T05:37:20+00:00

reading diveintopython3 came upon this and can’t deconstruction into smaller bits i understand. >>>

  • 0

reading diveintopython3 came upon this and can’t deconstruction into smaller bits i understand.

>>> a_dict = {'a': 1, 'b': 2, 'c': 3}                        
>>> {value:key for key, value in a_dict.items()}             
>>> {1: 'a', 2: 'b', 3: 'c'}

a_dict.items() creates a list. But value in a_dict.items() doesn’t make sense to me as value isn’t defined…

whats going on here?

  • 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-31T05:37:22+00:00Added an answer on May 31, 2026 at 5:37 am

    a_dict.keys() is a list of keys in your dictionary

    a_dict.values() is a list of values in your dictionary

    a_dict.items() is a list of (key, value) pairs

    {value:key for key, value in a_dict.items()} is a dict() comprehension which takes a_dict and switches its keys with its values, returning a new data structure as the result. If you look closely you can see that in the result part we have value:key but in the iteration part we have key, value — they switch! Very tricky, very pythonic.

    If there are no duplicate values in a_dict, this will result the inverse of the original dictionary. If there are duplicates, since all keys in the new dict must be unique, only one of the mappings of each of the duplicated values will survive.

    >>> a_dict = {'a': 1, 'b': 2, 'c': 3}
    >>> b_dict = {value:key for key, value in a_dict.items()}
    
    >>> a_dict.items()
    [('a', 1), ('c', 3), ('b', 2)]
    >>> b_dict.items()
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    >>> a_dict.keys()
    ['a', 'c', 'b']
    >>> b_dict.values()
    ['a', 'b', 'c']
    
    >>> a_dict.values()
    [1, 3, 2]
    >>> b_dict.keys()
    [1, 2, 3]
    
    # Try one with duplicates!
    >>> c_dict = {'a': 1, 'b': 2, 'c': 3, 'd':2}
    >>> d_dict = {value:key for key, value in c_dict.items()}
    
    # The number 2 can only map to one value;
    # the constructor seems to have encountered 'd' last
    >>> c_dict.items()
    [('a', 1), ('c', 3), ('b', 2), ('d', 2)]
    >>> d_dict.items()
    [(1, 'a'), (2, 'd'), (3, 'c')]
    

    Here’s a simpler example of a dict() comprehension if you’re having difficulty understanding how they work; type it in to your interactive editor and mess around with it!

    >>> e_dict = {n: str(n) * n for n in range(1, 30)}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through this , I came to the bit on default values for function
Reading this blog post it mentions you can get your DI container to automatically
Reading a blog entry by Eric Lippert , I came across this snippet: ...you're
reading the documentation for java org.w3c.dom.ls it seems as a Element only can be
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading this site, I've found this : [The] line private static final Foo INSTANCE
Reading the Google Docs API I find this: Downloading Files cannot be downloaded in
reading pylons documentations I did understand that websetup:setup_app is only called when the application
Reading Chomsky hierarchy ... ... I know regexp can't parse type-2 grammars (context-free grammars),

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.