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'}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

sanju saini
sanju saini

Written by sanju saini

Senior Software Engineer with 4.5+ years of experience in full-stack development, specializing in Python, Golang, PHP, Django, FastAPI, React, and RESTful APIs.

No responses yet

Write a response