• 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
  • Ruby
  • 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.  Set Widths of Columns in Word Document Table with python-docx
  • 2.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 3.  Using Python to Convert Decimal to String
  • 4.  Sorting with Lambda Functions in Python
  • 5.  Using Python to Read File Word by Word
  • 6.  Truncate Decimal from Number with Python math.trunc() Function
  • 7.  How to Find the Longest String in List in Python
  • 8.  Python tanh – Find Hyperbolic Tangent of Number Using math.tanh()
  • 9.  Using Python to Split a Number into Digits
  • 10.  Python issubset() Function – Check if Set is Subset of Another Set

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy