0% found this document useful (0 votes)
2 views3 pages

Complete Guide to Python Dictionaries

A dictionary in Python is a collection of key-value pairs that are unordered, mutable, and support fast lookups. It can be created using various methods and has unique, immutable keys. Common methods include accessing values, merging dictionaries, and modifying entries, with examples provided for practical usage.

Uploaded by

mmgsg103
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Complete Guide to Python Dictionaries

A dictionary in Python is a collection of key-value pairs that are unordered, mutable, and support fast lookups. It can be created using various methods and has unique, immutable keys. Common methods include accessing values, merging dictionaries, and modifying entries, with examples provided for practical usage.

Uploaded by

mmgsg103
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DICTIONARY – COMPLETE PYTHON NOTES

1. What is a Dictionary?

A dictionary is a collection of key–value pairs. Keys map to values.

2. Creating a Dictionary

• person = {"name": "Aarav", "city": "Delhi", "age": 20}

• info = dict(name="Aarav", city="Delhi", age=20)

• d = {} and then d["language"] = "Python"

3. Properties of Dictionaries

1. Unordered (preserves insertion order)

2. Key–value based structure

3. Keys must be unique

4. Keys must be immutable

5. Mutable structure

6. Fast lookup (O(1))

7. Dynamic size

4. Accessing Values (5 Methods)

• get(key)

• keys()

• values()

• items()

• Direct access: dict[key]

5. Dictionary Methods

1. get(key, default)

Example:

data = {"a":1, "b":2}

[Link]("c", "Not Found")

2. keys()
Returns all keys.

3. values()

Returns all values.

4. items()

Returns (key, value) pairs.

5. setdefault(key, default)

Adds key if not exists.

6. popitem()

Removes last key-value pair.

7. update()

Merges dictionaries.

8. clear()

Removes all items.

9. copy()

Creates a shallow copy.

6. Example Program

student = {

"name": "Rakshita",

"age": 18,

"skills": ["Python", "Web Dev"]

print([Link]("name"))

student["city"] = "Karnal"

[Link]({"age": 19})

[Link]()

print([Link]())

print([Link]())
print([Link]())

You might also like