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

Dictionary_Methods_Python

The document outlines important dictionary methods in Python, including adding, updating, removing, and accessing items. It covers methods such as pop, clear, get, and update, along with examples for each. Additionally, it discusses checking keys, copying dictionaries, merging, setting default values, creating dictionaries from keys, and using dictionary comprehension.

Uploaded by

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

Dictionary_Methods_Python

The document outlines important dictionary methods in Python, including adding, updating, removing, and accessing items. It covers methods such as pop, clear, get, and update, along with examples for each. Additionally, it discusses checking keys, copying dictionaries, merging, setting default values, creating dictionaries from keys, and using dictionary comprehension.

Uploaded by

aamansagarrr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Important Dictionary Methods in Python

1. Adding & Updating Items

- dict[key] = value: Adds or updates a key-value pair.

Example:

my_dict = {"name": "Alice"}

my_dict["age"] = 25 # Adds new key-value pair

my_dict["name"] = "Bob" # Updates existing key

2. Removing Items

- pop(key): Removes key and returns its value. Raises KeyError if key doesn't exist.

- popitem(): Removes and returns the last inserted key-value pair (LIFO order in Python 3.7+).

- clear(): Removes all key-value pairs.

- del dict[key]: Deletes key-value pair. Raises KeyError if key doesn't exist.

Example:

my_dict = {"name": "Alice", "age": 25}

my_dict.pop("age") # Returns 25

my_dict.clear() # Empties the dictionary

3. Accessing Items

- get(key, default): Returns value for the key; returns default if key isn't found.

- keys(): Returns a view of all keys.

- values(): Returns a view of all values.

- items(): Returns a view of all key-value pairs.

Example:

my_dict = {"name": "Alice", "age": 25}

print(my_dict.get("name")) # Alice
print(my_dict.get("address", "Not Found")) # Not Found

4. Checking Keys

- in: Checks if a key exists in the dictionary.

Example:

my_dict = {"name": "Alice"}

print("name" in my_dict) # True

5. Copying a Dictionary

- copy(): Returns a shallow copy of the dictionary.

Example:

original = {"name": "Alice"}

copy_dict = original.copy()

6. Merging Dictionaries

- update(other_dict): Adds key-value pairs from another dictionary, updating existing keys.

Example:

my_dict = {"name": "Alice"}

my_dict.update({"age": 25, "city": "NYC"})

7. Default Value Setting

- setdefault(key, default): Returns the value of the key if it exists, otherwise sets it to default and

returns it.

Example:

my_dict = {"name": "Alice"}

my_dict.setdefault("age", 25) # Sets "age" to 25


8. From Keys

- fromkeys(iterable, value): Creates a new dictionary from a list of keys with a specified value.

Example:

keys = ["name", "age", "city"]

new_dict = dict.fromkeys(keys, "Unknown")

9. Dictionary Comprehension

- Efficiently create dictionaries using loops.

Example:

squares = {x: x * x for x in range(1, 6)}

You might also like