Dictionary in Python

Python tutorial · PySpark.in

Dictionary

A Dictionary in Python is a powerful data structure used to store data in key–value pairs.

Syntax :

PLAINTEXT
1{ key: value }
▶ Output will appear here.

1. Creating a Dictionary

A dictionary is created using curly braces {} with key:value pairs.

PLAINTEXT
1student = {
2 "name": " Rohan",
3 "age": 20,
4 "course": "BCA"
5}
▶ Output will appear here.

2. Accessing Values

You can access values using keys, not indexes.

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
▶ Output will appear here.

Using .get() (safer Method)

If the key does not exist, .get() returns None instead of an error.

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
▶ Output will appear here.

3. Changing Values

Updating the value of an existing key in the dictionary.

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
9student["age"] = 23
10print(student)
▶ Output will appear here.

4. Adding New Key–Value Pair

Creating a new entry in the dictionary using a new key.

PYTHON
1student["city"] = "Delhi"
2print(student)
▶ Output will appear here.

5. Removing Items from a Dictionary

pop(key)Removes a specific key and its value from the dictionary.

popitem  Removes the last inserted key–value pair

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
9student["age"] = 23
10print(student)
11student.pop("age")
▶ Output will appear here.

remove last inserted key

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
9student["age"] = 23
10print(student)
11student.popitem()
▶ Output will appear here.

delete using del

Deletes a specific key or the entire dictionary.

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
9student["age"] = 23
10print(student)
11del student["course"]
▶ Output will appear here.

clear all

Removes all key–value pairs, leaving the dictionary empty.

PYTHON
1student = {
2 "name": "Rohan",
3 "age": 20,
4 "course": "BCA"
5}
6print(student["name"])
7print(student["age"])
8print(student.get("course"))
9student["age"] = 23
10print(student)
11student.clear()
▶ Output will appear here.

6. Dictionary Methods

Keys()->get all Keys

PYTHON
1print(student.keys())
▶ Output will appear here.

values()->get all values

PYTHON
1print(student.values())
▶ Output will appear here.

items()-> get both keys & values

PYTHON
1print(student.items())
▶ Output will appear here.

7. Looping Through a Dictionary

Loop keys

Iterating over all keys in the dictionary using a loop.

PLAINTEXT
1for key in student:
2 print(key)
▶ Output will appear here.

Loop values

Iterating over all values in the dictionary using a loop.

PLAINTEXT
1for value in student.values():
2 print(value)
▶ Output will appear here.

Loop both

Iterating using both key and value together via items()

PLAINTEXT
1for key, value in student.items():
2 print(key, ":", value)
▶ Output will appear here.

8. Dictionary with Different Data Types

A dictionary can store values of different data types including lists, numbers, booleans, and other dictionaries.

PYTHON
1info = {
2 "name": "Megha",
3 "age": 22,
4 "is_student": True,
5 "marks": [90, 88, 92],
6 "address": {"city": "Delhi", "pin": 110001}
7}
8print(info["marks"][1]) # 88
9print(info["address"]["city"]) # Delhi
▶ Output will appear here.

9. Nested Dictionary

A dictionary inside another dictionary used to store structured data.

PYTHON
1students = {
2 "s1": {"name": "Megha", "age": 22},
3 "s2": {"name": "Rahul", "age": 21}
4}
5print(students["s1"]["name"])
▶ Output will appear here.

10. Length of Dictionary

The len() function returns the total number of key–value pairs.

PYTHON
1print(len(student))
▶ Output will appear here.

11. Update Method

Updates existing keys or adds new key–value pairs to the dictionary.

PLAINTEXT
1student.update({"age": 24, "city": "Pune"})
▶ Output will appear here.

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges