search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

collections | OrderedDict constructor

Programming
chevron_right
Python
chevron_right
Standard Library
chevron_right
collections
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer Python
Tags

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.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!