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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:01+00:00 2026-06-13T22:55:01+00:00

Hi want to have a ordered dictionary with keys having list of values. from

  • 0

Hi want to have a ordered dictionary with keys having list of values.

from below code i could able to get a dictionary with list of keys but ordered of insertion is missing.

from collections import defaultdict

keys=['blk','pri','ani']
vals1=['blocking','primary','anim']
vals2=['S1','S2','S3']
dic = defaultdict(list)

i=0

for key in keys:
    dic[key].append(vals1[i])
    dic[key].append(vals2[i])

    i += 1

print dic

i get the following result

defaultdict(<type 'list'>, {'pri': ['primary', 'S2'], 'ani': ['anim', 'S3'], 'blk': ['blocking', 'S1']})

here i lost insert order.

I know defaultdict object in Python are unordered by definition.

And i know we need to Use OrderedDict if you need the order in which values were inserted (it’s available in Python 2.7 and 3.x)

So changed my code as below

from below code i could able to get what i need.

from collections import defaultdict,OrderedDict

keys=['blk','pri','ani']
vals1=['blocking','primary','anim']
vals2=['S1','S2','S3']
dic = OrderedDict(defaultdict(list))

i=0

for key in keys:
    dic[key].append(vals1[i])
    dic[key].append(vals2[i])

    i += 1

print dic

and now i get the below error

Traceback (most recent call last):
  File "Dict.py", line 18, in <module>
    dic[key].append(vals1[i])
KeyError: 'blk' 

Can any one tell me how to get what i am trying.

  • 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-13T22:55:02+00:00Added an answer on June 13, 2026 at 10:55 pm

    If you really want the “Ordered” and “default” behavior, I think I would create a custom dictionary class to handle everything for me:

    from collections import OrderedDict
    
    class DefaultListOrderedDict(OrderedDict):
        def __missing__(self,k):
            self[k] = []
            return self[k]
    
    keys=['blk','pri','ani']
    vals1=['blocking','primary','anim']
    vals2=['S1','S2','S3']
    dic = DefaultListOrderedDict()
    for i,key in enumerate(keys):
        dic[key].append(vals1[i])
        dic[key].append(vals2[i])
    
    print dic
    

    This will behave more like the defaultdict than the other solutions, but it will retain it’s order, and it really isn’t a whole lot of code :). Finally, you could even override __init__ to allow the user to pass any “factory” they wanted (rather than hard-coding list). I’ll leave that as an exercise for the interested reader. Python is so cool.

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

Sidebar

Related Questions

Suppose I have an ordered list of float (ascending). I want to remove from
I have an ordered list, but I want to number is manually in a
I have an ordered list like 0,1,2, 6,7, 10 I want to get the
I have an ordered list in my Word document, and I want to add
I have an ordered list like this: var list = new List<String>(){aaa,aab,aac,baa,bab,bac}; I want
I have an ordered list (a dictionary - 100K words) and many words to
I currently have an ordered list that I want to markup using the new
let's say I have a list b=['m','NN'] and a dictionary dict={'b':['NN','m','big']} and I want
I have ordered alphabetically hte list of customer on my Custom List view, but
So basically I have an ordered binary tree, and I want to be able

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.