0% found this document useful (0 votes)
6 views

Dictionary Python

Uploaded by

Nivedika Namburi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Dictionary Python

Uploaded by

Nivedika Namburi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

A dictionary in Python is an unordered collection of data in a key-value pair format.

Each key is
unique, and the value associated with the key can be of any data type.
Syntax
A dictionary is created by placing a comma-separated list of key-value pairs inside curly braces
{}, with a colon: separating each key from its value.
# Example dictionary
student = {
"name": "Alice",
"age": 21,
"grade": "A",
"subjects": ["Math", "Physics"]
}
Key Features of Dictionaries:
 Keys must be immutable (strings, numbers, or tuples), and values can be of any type.
 They are unordered as of Python 3.6+ (although insertion order is preserved).
 Dictionaries are mutable, meaning they can be changed after creation.

Common Dictionary Operations:


1. Creating a Dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
2. Accessing Values
You can access a value by using its corresponding key.
print(my_dict["name"]) # Output: John
3. Adding or Updating Key-Value Pairs
python
Copy code
my_dict["email"] = "[email protected]" # Adding a new key-value pair
my_dict["age"] = 26 # Updating an existing key-value pair
4. Removing Key-Value Pairs
 Using pop(key) – Removes the key and returns its value.
 Using del keyword – Deletes the key-value pair.
my_dict.pop("age") # Removes the "age" key
del my_dict["city"] # Deletes the "city" key
5. Checking if a Key Exists
if "name" in my_dict:
print("Key exists")
6. Iterating Over a Dictionary
 Keys:
for key in my_dict:
print(key)
 Values:
for value in my_dict.values():
print(value)
 Key-Value Pairs:
for key, value in my_dict.items():
print(key, value)
7. Dictionary Methods
 keys() – Returns a list of dictionary keys.
 values() – Returns a list of dictionary values.
 items() – Returns a list of key-value pairs (tuples).
print(my_dict.keys()) # Output: dict_keys(['name', 'email'])
print(my_dict.values()) # Output: dict_values(['John', '[email protected]'])
print(my_dict.items()) # Output: dict_items([('name', 'John'), ('email', '[email protected]')])
8. Merging Dictionaries
Use the update() method to merge two dictionaries.
dict1 = {"name": "John", "age": 25}
dict2 = {"city": "New York", "email": "[email protected]"}
dict1.update(dict2)
print(dict1) # Output: {'name': 'John', 'age': 25, 'city': 'New York', 'email': '[email protected]'}

Example Program: Dictionary in Action


Here’s an example that demonstrates the use of dictionary operations:
# A dictionary storing student details
student = {
"name": "Alice",
"age": 21,
"marks": {
"Math": 85,
"Physics": 90,
"Chemistry": 88
}
}

# Accessing data
print(student["name"]) # Output: Alice
print(student["marks"]["Math"]) # Output: 85

# Updating marks
student["marks"]["Math"] = 95
print(student["marks"]["Math"]) # Output: 95

# Adding a new subject


student["marks"]["English"] = 89
# Removing the Chemistry subject
student["marks"].pop("Chemistry")

# Printing updated dictionary


print(student)
Output:
python
Copy code
{
'name': 'Alice',
'age': 21,
'marks': {
'Math': 95,
'Physics': 90,
'English': 89
}
}

Summary
 Dictionaries store key-value pairs.
 You can perform various operations like accessing, updating, deleting, and iterating over
dictionary elements.
 Dictionaries are mutable and allow dynamic modifications to the data.

You might also like