Python Dictionaries Overview

Dictionaries are another essential data type in Python, and they allow you to store a collection of key-value pairs. A dictionary is created using curly braces {}, and the key-value pairs are separated by commas. The keys and values in a dictionary can be of any data type, including strings, integers, floats, and even lists or other dictionaries.

Here is an example of a dictionary in Python:

{ "name": "John", "age": 30, "city": "New York" }

You can manipulate dictionaries in a variety of ways in Python. Here are some common dictionary operations:

Accessing: You can access the value of a dictionary item using its key. For example:

person = { "name": "John", "age": 30, "city": "New York" } name = person["name"] print(name) # Output: "John"

You can also use the get() method to access the value of a dictionary item. This method allows you to specify a default value to return if the key is not found in the dictionary. For example:

person = { "name": "John", "age": 30, "city": "New York" } name = person.get("name", "Unknown") print(name) # Output: "John" age = person.get("age", 0) print(age) # Output: 30 country = person.get("country", "Unknown") print(country) # Output: "Unknown"

Adding: You can add a new item to a dictionary using the assignment operator =. For example:

person = { "name": "John", "age": 30, "city": "New York" } person["country"] = "United States" print(person) # Output: { "name": "John", "age": 30, "city": "New York", "country": "United States" }

Updating: You can update the value of an existing dictionary item using the assignment operator =. For example:

person = { "name": "John", "age": 30, "city": "New York" } person["age"] = 31 print(person) # Output: { "name": "John", "age": 31, "city": "New York" }

Deleting: You can delete an item from a dictionary using the del statement or the pop() method. The del statement allows you to delete an item by its key, while the pop() method allows you to delete an item by its key and return its value. For example:

person = { "name": "John", "age": 30, "city": "New York" } del person["age"] print(person) # Output: { "name": "John", "city": "New York" } person = { "name": "John", "age": 30, "city": "New York" } age = person.pop("age") print(age) # Output: 30

Iterating: You can iterate over the keys, values, or key-value pairs in a dictionary using a for loop. For example:

person = { "name": "John", "age": 30, "city": "New York" } # Iterating over keys for key in person: print(key) # Output: "name", "age", "city" # Iterating over values for value in person.values(): print(value) # Output: "John", 30, "New York" # Iterating over key-value pairs for key, value in person.items(): print(key, value) # Output: "name", "John", "age", 30, "city", "New York"

There are many more operations you can perform on dictionaries in Python, but these are some of the most commonly used. I hope this gives you a good overview of how to work with dictionaries in Python.


No comments

Powered by Blogger.