Is It Possible for a Dictionary to Have Several Keys with Identical Values? With 5 Examples

Is It Possible for a Dictionary to Have Several Keys with Identical Values? With 5 Examples

Yes, in Python, a dictionary can have multiple keys with the same value. Here are five examples demonstrating this:

  1. Example 1:
my_dict = {'a': 1, 'b': 2, 'c': 2, 'd': 3}

In this dictionary, both keys 'b' and 'c' have the same value 2.

  1. Example 2:
my_dict = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange', 'grape': 'purple', 'plum': 'purple'}

In this dictionary, both keys 'grape' and 'plum' have the same value 'purple'.

  1. Example 3:
my_dict = {'John': 25, 'Alice': 30, 'Bob': 25, 'Charlie': 30}

In this dictionary, both 'John' and 'Bob' have the same age 25, and both 'Alice' and 'Charlie' have the same age 30.

  1. Example 4:
my_dict = {'a': [1, 2], 'b': [3, 4], 'c': [1, 2]}

In this dictionary, both keys 'a' and 'c' have the same list [1, 2] as their value.

  1. Example 5:
my_dict = {'x': True, 'y': False, 'z': True, 'w': False}

In this dictionary, both 'x' and 'z' have the value True, and both 'y' and 'w' have the value False.

These examples illustrate how multiple keys can share the same value in a Python dictionary.