Python Dictionaries Cheat Sheet
Python Dictionaries Cheat Sheet
DIGYQUANT ANALYTICS
Python Dictionaries
CHEATSHEET
A dictionary is an unordered collection of key-value pairs, where each key is unique and
associated with a corresponding value. Dictionaries are one of the built-in data types in Python
and are represented using curly braces {}.
Fromkeys Create a new dictionary with specified keys and a default value.
Get Return the value of the specified key or a default value if the key is not found
Pop Remove and return the value associated with the specified key.
Update Update the dictionary with key-value pairs from another dictionary or iterable.
Fromkeys Create a new dictionary with specified keys and a default value.
print(fruits_count)
# Output: {'apple': 10, 'banana': 10, 'orange': 10, 'grape': 10}
Get Return the value of the specified key or a default value if the key is not found
Training Program
MACHINE LEARNING
ARTIFICIAL INTELLIGENCE
12 August
ENROLL NOW
WWW.DIGYQUANT.COM
DIGYQUANT ANALYTICS
print(student_items_list)
# Output: [('A', 20), ('B', 22), ('C', 19), ('D', 21)]
DigyQuant Benefits
1:1 Mentorship from In-depth Q&A and
industry experts Clarification
print(student_keys_list)
# Output: ['A', 'B', 'C', 'D']
Pop Remove and return the value associated with the specified key.
# Remove and return the value associated with the key "Bob"
removed_age = student_ages.pop("B")
# Remove and return the last key-value pair from the dictionary
last_item = student_ages.popitem()
print(student_ages_list)
# Output: [20, 22, 19, 21]
All mentors with more than a decade experience in Data Science domain
DIGYQUANT ANALYTICS
Update Update the dictionary with key-value pairs from another dictionary or iterable.
print(dict1)
# Output: {'A': 20, 'B': 22, 'C': 19, 'D': 21}
print(dict1)
# Output: {'A': 20, 'B': 22, 'C': 19, 'D': 21, 'E': 23, 'F': 25}
# Create a dictionary
original_dict = {"A": 20, "B": 22, "C": 19}
print(original_dict)
# Output: {'A': 20, 'B': 22, 'C': 19}
print(copied_dict)
# Output: {'A': 20, 'B': 22, 'C': 19, 'D': 21}
print(student_ages)
# Output: {}
All mentors with more than a decade experience in Data Science domain
DIGYQUANT
ANALYTICS
Training Program
MACHINE LEARNING
ARTIFICIAL INTELLIGENCE
12 August
ENROLL NOW
WWW.DIGYQUANT.COM