• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Why Dictionaries Can’t Have Duplicate Keys in Python

Why Dictionaries Can’t Have Duplicate Keys in Python

June 6, 2022 Leave a Comment

In Python, dictionary variables cannot have duplicate keys as by definition, they cannot have duplicate keys.

If you try to define a dictionary with duplicate keys, the last key will be kept with all other duplicate keys removed.

d = {"name":"Bobby", "name":"Sam", "name":"Alex", "height":65, "height":100, "income":65}

print(d)

#Output:
{'name': 'Alex', 'height': 100, 'income': 65}

In Python, dictionaries are a collection of key/value pairs separated by commas.

When working with dictionaries, knowing the definitions and properties can be useful when you are trying to troubleshoot or design a process.

One such dictionary property is that dictionary items are ordered, changeable and do not allow duplicates.

Therefore, you cannot have duplicate keys.

If you try to define a dictionary with duplicate keys, the last key will be kept with all other duplicate keys removed.

d = {"name":"Bobby", "name":"Sam", "name":"Alex", "height":65, "height":100, "income":65}

print(d)

#Output:
{'name': 'Alex', 'height': 100, 'income': 65}

Remove Duplicate Values from Dictionary by Inverting Dictionary in Python

One case where you might have an issue with duplicate keys is if you try to invert a dictionary with duplicate values in Python.

Since dictionaries can only have a unique set of keys, when you go to invert it, you will remove any duplicate values.

Below is an example of inverting a dictionary with duplicate values. You can see that only one key is kept for each value.

d = {"name":"Bobby", "age":20, "credits":20, "height":65, "weight":65, "income":65}

d_inverted = {value: key for key, value in d.items()}

#Output:
{'Bobby': 'name', 20: 'credits', 65: 'income'}

Hopefully this article has been useful for you to learn why dictionary variables in Python cannot have duplicate keys.

Other Articles You'll Also Like:

  • 1.  pandas variance – Compute Variance of Variables in DataFrame
  • 2.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 3.  Using Python to Get Queue Size
  • 4.  Get Difference Between datetime Variables in Python
  • 5.  Creating a Random Color Turtle in Python
  • 6.  Get First Character of String in Python
  • 7.  Set Widths of Columns in Word Document Table with python-docx
  • 8.  Check if String is Date in Python
  • 9.  e in Python – Using Math Module to Get Euler’s Constant e
  • 10.  Read Last N Lines of File in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy