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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:24:06+00:00 2026-05-15T06:24:06+00:00

In Python, I need a dictionary object which looks like: {‘a’: 10, ‘b’: 20,

  • 0

In Python, I need a dictionary object which looks like:

{'a': 10, 'b': 20, 'c': 10, 'd': 10, 'e': 20}

I’ve been able to get this successfully by combining the dict.update() and dict.fromkeys() functions like so:

myDict = {}
myDict.update(dict.fromkeys(['a', 'c', 'd'], 10))
myDict.update(dict.fromkeys(['b', 'e'], 20))

However, because the code is being written for novice users who may need to make add keys/values on occasion, I’d prefer a simple bare-bones (Perl-like) syntax such as:

myDict = {}
myDict['a', 'c', 'd'] = 10
myDict['b', 'e'] = 20

This, however, gives me:

myDict = {('a', 'c', 'd'): 10, ('b', 'e'): 20}

Is there a way I can simplify my first example (using dict.update() and dict.fromkeys()) further, and get the dict object I’m looking for?

Or, alternatively, if I have a dict with tuples as in my second example, is there an easy way for me to do a lookup such as myDict['c'] or myDict.get('c') and get the value 10?

  • 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-15T06:24:07+00:00Added an answer on May 15, 2026 at 6:24 am

    I would say what you have is very simple, you could slightly improve it to be:

    my_dict = dict.fromkeys(['a', 'c', 'd'], 10)
    my_dict.update(dict.fromkeys(['b', 'e'], 20))
    

    If your keys are tuple you could do:

    >>> my_dict = {('a', 'c', 'd'): 10, ('b', 'e'): 20}
    >>> next(v for k, v in my_dict.items() if 'c' in k)      # use .iteritems() python-2.x
    10
    

    This is, of course, will return first encountered value, key for which contains given element.

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

Sidebar

Related Questions

I have a process that pickles a dictionary using Python 3.2. I then need
I am incredibly new to Python and I really need to be able to
This is a great primer but doesn't answer what I need: Combining two sorted
I have some Python code where I need to call upon a dictionary entry
I need to sort a Python dictionary by keys, when keys are floating point
In Python, given a dictionary like { 'VENEZUELA': 'CARACAS', 'CANADA': 'OTTAWA' } How can
Consider this dictionary format. {'KEY1':{'name':'google','date':20100701,'downloads':0}, 'KEY2':{'name':'chrome','date':20071010,'downloads':0}, 'KEY3':{'name':'python','date':20100710,'downloads':100}} I'd like the dictionary sorted by downloads
I need to import a very large dictionary into python and I'm running into
My Python program receives JSON data, and I need to get bits of information
Possible Duplicate: Python: Sort a dictionary by value I need to sort by values

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.