• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Ordered Dictionary / How to Create Ordered Dictionary in Python?

How to Create Ordered Dictionary in Python?

September 12, 2019 by cmdlinetips

Dictionary in Python is one of the most useful core data structures in Python. Sometimes, you may want to create a dictionary and also maintain the order of items you inserted when you are iterating the keys.

Python’s collections module has OrderedDict that lets you create a ordered dictionary. Let us see an example of ordered dictionary and how it differs from regular dictionary in Python.

import collections

Create Ordered Dictionary

We can create ordered dictionary using OrderedDict function in collections.

ordered_d = collections.OrderedDict(one=1, two=2, three=3)

Ordered dictionary preserves the insertion order.

ordered_d
OrderedDict([('one', 1), ('two', 2), ('three', 3)])

We can iterate through the dictionary items and see that the order is preserved.

for k,v in ordered_d.items():
    print(k,v)

one 1
two 2
three 3

If you are using Python version 3.6+, the core dictionary data structure in Python also preserves the insertion order. Python documentation claims that

The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Add a New Column Using a Dictionary in Pandas Data Frame ? Default ThumbnailGetting Started With Python Dictionaries: Python Tips Default Thumbnail5 Examples Using Dict Comprehension in Python Default ThumbnailHow To Create a Pandas Data Frame From Lists?

Filed Under: Ordered Dictionary Tagged With: Ordered Dictionary, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version