Livoa LogoLivoa
Python Dictionary Overview Flowchart
1. What is a Dictionary?


A dictionary stores key–value pairs.

Example:

d = {"name": "Tanu", "age": 20, "city": "Delhi"}
2. Properties of Dictionary Keys


  1. Keys must be unique
  2. Keys must be immutable (str, int, tuple)
  3. Values can be any data type and can repeat
  4. Dictionary is unordered (in Python 3.7+ it maintains insertion order)
3. Basic Operations on Dictionary


Accessing Values


d = {"name": "Tanu", "age": 20}
print(d["name"])

Output: Tanu

Adding a New Key–Value


d["city"] = "Kolkata"
Updating a Value


d["age"] = 21
Deleting a Key–Value


Using del

del d["age"]

Using pop()

d.pop("city")

Using popitem() (removes last inserted item)

d.popitem()
4. Built-in Dictionary Functions


len()


len({"a":1, "b":2})

Output: 2

str()


Converts dictionary to string.

5. Built-in Dictionary Methods


1. keys()


d = {"a":1, "b":2}
print(d.keys())

Output: dict_keys(['a', 'b'])

2. values()


print(d.values())
3. items() (key–value pairs)


print(d.items())
4. get() (Safer than square brackets)


d.get("age")
5. update()


d.update({"city": "Delhi"})
6. pop(key) (Removes The Key-Value Pair)


python

by tanu

0
0 uses