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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:50:34+00:00 2026-05-27T02:50:34+00:00

Example of my Code: dictionary={ ‘key1’ : ‘value1’ , ‘key2’ : ‘value2’ , ‘key1’

  • 0

Example of my Code:

dictionary={ 'key1' : 'value1' , 'key2' : 'value2' , 'key1' : 'value3' }

x=dictionary['key1']

print(x)

This prints ONLY value3. I want it to print both value3 and value1, since they
both correlate to key1. How can I do this?

  • 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-27T02:50:35+00:00Added an answer on May 27, 2026 at 2:50 am

    You cannot. The keys are added to the dictionary in the order you specified and the second occurence of ‘key1’ overwrites the previous one.

    This is just as though you had written:

    x = 1
    x = 2
    

    and then asked how you could print both values of x.

    To record multiple values the easiest way is to use a defaultdict and build lists of values:

    >>> import collections
    >>> dictionary = collections.defaultdict(list)
    >>> dictionary['key1'].append('value1')
    >>> dictionary['key2'].append('value2')
    >>> dictionary['key1'].append('value3')
    >>> print(dictionary['key1'])
    ['value1', 'value3']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like my dictionary to be case insensitive. I have this example code: text
Is it possible to set code behind a resource dictionary in WPF. For example
Many C++ books contain example code like this... std::cout << Test line << std::endl;
There is this example code, but then it starts talking about millisecond / nanosecond
This app is using iOS 5's built-in dictionary with UISearchBar. I want to include
When are function-local variables are created? For example, in the following code is dictionary
I have this sample code. List<Dictionary<string,string>> objects = new List<Dictionary<string,string>>(); foreach (string url in
I want to instantiate a generic collection (a Dictionary in this case) but in
In the example code below, I get this error : Element TestSerializeDictionary123.Customer.CustomProperties vom Typ
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1

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.