bytes
tutorials
python
dictionary in python
Overview
Dictionaries in Python are a capable and adaptable data structure that permits you to store and manipulate collections of key-value pairs. In this lesson, we'll cover the essentials of dictionaries in Python, including how to create and access dictionaries, include and remove elements, and perform common operations.
Introduction to Dictionary
"In Python, dictionaries are represented by curly braces ({}) encasing a comma-separated list of key-value pairs, where each key is separated from its corresponding value by a colon (: )." In Python, a dictionary could be a collection of key-value pairs, where each key is one of a kind and related to a particular value. An industry example to explain a dictionary in Python is in the customer relationship management (CRM) software industry. In CRM software, customer information is stored and managed using dictionaries. For instance, let's say that a CRM software company has a dictionary containing customer information for a particular client. The Dictionary may look something like this:
customer_info = {
"Name": "John Smith",
"Age": 35,
"Email": "johnsmith@example.com",
"Phone": "555-123-4567",
"Address": "123 Main Street",
"City": "Anytown",
"State": "CA",
"Zip": "12345"
}
In this example, the Dictionary contains various customer information, such as name, age, email, phone, address, city, state, and zip code. The keys in the Dictionary (e.g., "Title", "Age", "Email", etc.) correspond to the type of data being put away, and the values (e.g., "John Smith", 35, "johnsmith@example.com", etc.) are the genuine information points.
Within the CRM computer program industry, dictionaries are valuable since they permit customer information to be stored and recovered rapidly and effectively. The software can rapidly look for and recover the required data by utilizing unique keys to distinguish each piece of data.
Creating and Accessing Dictionaries
Python creates a dictionary using curly braces {} and key-value pairs separated by colons. For example:
# Creating a dictionary of ages
ages = {"Alice": 28, "Bob": 35, "Charlie": 42}
# Creating an empty dictionary
empty_dict = {}
To access an element in a dictionary, you can use square brackets [] and the element's key. For example:
# Accessing the age of Alice
alice_age = ages["Alice"]
print(alice_age)
If the key does not exist in the Dictionary, you will get a KeyError. To avoid this, you can use the get() method, which returns None if the key does not exist:
# Using the get() method
bob_age = ages.get("Bob")
print(bob_age)
# Using a default value with get()
david_age = ages.get("David", 0)
print(david_age)
Adding, Updating and Removing Elements
To add a new key-value pair to a dictionary, you can use square brackets [] and assign a value to the new key:
# Adding a new element to the dictionary
ages["David"] = 21
To update an existing element, you can use the same syntax:
# Updating an element in the dictionary
ages["Bob"] = 36
To remove an element from a dictionary, you can use the del keyword or the pop() method:
# Removing an element with del
del ages["Charlie"]
# Removing an element with pop()
alice_age = ages.pop("Alice")
Common Operations
Dictionaries in Python have many useful methods and operations. Here are a few examples:
Here's an example of how to use some of these operations:
# Printing all the keys
print(ages.keys())
# Printing all the values
print(ages.values())
# Printing all the key-value pairs
print(ages.items())
# Printing the number of elements
print(len(ages))
# Checking if a key exists
if "David" in ages:
print("David's age is", ages["David"])
else:
print("David is not in the dictionary")
Few things to keep in mind while making a dictionary
Conclusion
Dictionaries in Python are flexible and effective data structures that can be utilized for a wide assortment of applications. They permit you to store and manipulate collections of key-value pairs and give numerous valuable methods and operations for working with those collections. Whether you're working with huge datasets or little scripts, dictionaries are a basic tool in any Python programmer's toolkit.
Key Takeaways
Quiz
Answer: d. Keys in a dictionary must be unique and immutable.
Answer: b. unique
Answer: d. both b and c
Answer:a. pop()
Answer: a.True