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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:58:34+00:00 2026-06-06T03:58:34+00:00

is there any way to store duplicate keys in a dictionary? I have a

  • 0

is there any way to store duplicate keys in a dictionary?

I have a specific requirement to form pairs of requests and responses.

Requests from a particular node to another particular node form same keys. I need to store both those.

But if I tried to add them to dictionary, first one is being replaced by second. Is there any way?

  • 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-06T03:58:36+00:00Added an answer on June 6, 2026 at 3:58 am

    I can think of two simple options, assuming you want to keep using a dictionary.

    1. You could map keys to lists of items. A defaultdict from the collections module makes this easy.

      >>> import collections
      >>> data = collections.defaultdict(list)
      >>> for k, v in (('a', 'b'), ('a', 'c'), ('b', 'c')):
      ...     data[k].append(v)
      ... 
      >>> data
      defaultdict(<type 'list'>, {'a': ['b', 'c'], 'b': ['c']})
      
    2. You could use additional data to disambiguate the keys. This could be a timestamp, a unique id number, or something else. This has the advantage of preserving a one-to-one relationship between keys and values, and the disadvantage of making lookup more complex, since you always have to specify an id. The example below shows how this might work; whether it’s good for you depends on the problem domain:

      >>> for k, v in (('a', 'b'), ('a', 'c'), ('b', 'c')):
      ...     i = 0
      ...     while (k, i) in data:
      ...         i += 1
      ...     data[(k, i)] = v
      ... 
      >>> data
      {('a', 1): 'c', ('b', 0): 'c', ('a', 0): 'b'}
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to store a session variable in OpenEdge SQL similar to
Is there any way to store the generic parameter type passed in at construction
Is there any way to store file creation time in linux? May be filesystem
is there any easy way of store XML data into core data? Currently, my
I'd like to know if there is any way to store a series of
Is there any way to find out or store which app is accessing an
Is there any way to pre compile stored procedures in SQL Server? My requirement
Possible Duplicate: Is there any way to detect strings like putjbtghguhjjjanika? php check if
Is there a way to delete duplicate records based on two fields? I have
Is there any way to retrieve the names of the stored procedures by some

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.