PYTHON DICTIONARY

PYTHON DICTIONARY

PYTHON DICTIONARY

PYTHON DICTIONARY: 

A dictionary is a built-in data structure that stores key-value pairs. It is denoted by curly braces {} and each key-value pair is separated by a colon :. Here’s an example of creating a dictionary in Python:

				
					# Creating a dictionary
person = {"name": "John", "age": 30, "city": "New York"}

# Accessing values in a dictionary
print(person["name"])  # Output: John
print(person["age"])  # Output: 30

				
			

In this example, we create a dictionary named person with three key-value pairs: “name”: “John”, “age”: 30, and “city”: “New York”. We can access the values of each key by using the square bracket notation followed by the key name.

Python dictionaries are mutable, which means we can add, remove, or modify key-value pairs after the dictionary is created. Here are some examples of modifying a dictionary:

				
					# Adding a new key-value pair
person["occupation"] = "Engineer"
print(person)  # Output: {"name": "John", "age": 30, "city": "New York", "occupation": "Engineer"}

# Modifying an existing value
person["age"] = 35
print(person)  # Output: {"name": "John", "age": 35, "city": "New York", "occupation": "Engineer"}

# Removing a key-value pair
del person["occupation"]
print(person)  # Output: {"name": "John", "age": 35, "city": "New York"}

				
			

Python dictionaries also have many built-in methods for working with keys and values, such as keys(), values(), items(), get(), and update(). These methods make it easy to manipulate dictionaries and perform common operations.

Built-in Dictionary Functions & Methods

Python dictionaries come with several built-in functions and methods that make it easy to work with key-value pairs. Here are some of the most commonly used functions and methods:

  1. len(dict): returns the number of items in the dictionary
  2. str(dict): returns a string representation of the dictionary
  3. clear(): removes all items from the dictionary
  4. copy(): returns a shallow copy of the dictionary
  5. fromkeys(keys[, value]): creates a new dictionary with keys from a sequence and values set to a default value (if provided)
  6. get(key[, default]): returns the value for a key if it exists in the dictionary, otherwise returns a default value (if provided) or None
  7. key in dict: returns True if the key exists in the dictionary, otherwise returns False
  8. items(): returns a view object containing the key-value pairs of the dictionary
  9. keys(): returns a view object containing the keys of the dictionary
  10. values(): returns a view object containing the values of the dictionary
  11. pop(key[, default]): removes and returns the value for a key if it exists in the dictionary, otherwise returns a default value (if provided) or raises a KeyError
  12. popitem(): removes and returns an arbitrary key-value pair from the dictionary
  13. setdefault(key[, default]): returns the value for a key if it exists in the dictionary, otherwise sets the value to a default value (if provided) and returns it
  14. update(dict2): updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs
  15. sorted(dict): returns a new sorted list of the dictionary’s keys

These functions and methods are powerful tools for manipulating dictionaries in Python, and understanding how they work can help you write more efficient and effective code.

Join To Get Our Newsletter
Spread the love