What can be dictionary keys in python?

sanju saini
Feb 2, 2023

--

Photo by Rubaitul Azad on Unsplash

In Python, a dictionary key can be any hashable data type, such as integers, strings, tuples, or other objects that are defined as hashable. The keys in a dictionary must be unique, and the values can be of any type.

For example:

# using string as a key
dict = {'name': 'John', 'age': 32}

# using integer as a key
dict = {1: 'one', 2: 'two'}

# using tuple as a key
dict = {(1, 2): 'tuple key'}

--

--