search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

collections | OrderedDict constructor

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

An OrderedDict object is a subclass of a standard dict object. OrderedDict objects retain the order of keys in the order they were inserted (which dict objects also now support since Python 3.6).

Key advantages of OrderedDict over dict:

  • Methods available specifically for reordering items such as move_to_end(~) and popitem(~)

  • Performing equality tests where the order of items in the dictionary is important

  • Backward compatibility for versions earlier than Python 3.6

Parameters

1. items | sequence | optional

Key-value pairs to initialize the OrderedDict object with.

Return value

An OrderedDict object.

Examples

Basic usage

To initialize an empty OrderedDict object:

from collections import OrderedDict
od = OrderedDict() #Initialise OrderedDict object

#Add key-value pairs to the empty OrderedDict
od['key1'] = 1
od['key2'] = 2
print(od)
OrderedDict([('key1', 1), ('key2', 2)])

Keyword arguments

To initialize an OrderedDict using keyword arguments:

from collections import OrderedDict
od = OrderedDict(key1=1, key2=2) #Initialise OrderedDict object
print(od)
OrderedDict([('key1', 1), ('key2', 2)])

Sequence

To initialize an OrderedDict using a sequence of key-value pairs:

from collections import OrderedDict
od = OrderedDict([('key1', 1), ('key2', 2)])
print(od)
OrderedDict([('key1', 1), ('key2', 2)])

Set

To initialize an OrderedDict using a set:

from collections import OrderedDict
od = OrderedDict({("key3", 3), ("key2", 2), ("key1", 1)})
print(od)
OrderedDict([('key1', 1), ('key2', 2), ('key3', 3)])

When using a set to initialize an OrderedDict, the order of items is unknown until the OrderedDict is created.

Dictionary

To initialize an OrderedDict using a dict:

from collections import OrderedDict
od = OrderedDict({"key1": 1, "key2": 2, "key3": 3})
print(od)
OrderedDict([('key1', 1), ('key2', 2), ('key3', 3)])

If you are using Python 3.6 or greater, the order of items in OrderedDict will be the same as the order of items in the dict used to initialize. In versions lower than Python 3.6, the order of items is not known until the OrderedDict is initialized.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!