Python Dictionaries

There are a number of data structures available in Python, which makes it an appealing and versatile programming language. The “Dictionary” is a frequently utilized data structure in Python. This article explores Python dictionaries, including how to create, modify, and access dictionary items using numerous examples.

Dictionaries in Python

A “dictionary” in Python is composed of “key-value” pairs, where every key is distinct and mapped/corresponds to the appropriate value. “Dictionaries” are randomly arranged, so the order of items is not preserved.

Dictionaries are an extremely useful tool in Python, as they are used to store and retrieve data in a convenient and time-saving manner. They are commonly used to represent real-world objects, such as people or products, where each key represents a unique identifier and each value refers to a specific attribute or property.

How to Create Dictionaries in Python?

The preferred way to create/make a dictionary in Python is to employ “curly braces {}” or the built-in “dict()” function.

Example

The below code is used to create a dictionary in Python:

# Using curly braces
dict_value = {'Name': 'Joseph', 'Age': 30, 'Height': 6.3}
print('Dictionary is Created Using the Curly Braces: ',dict_value)
print(type(dict_value))
# Using dict() function
dict_value = dict(Name='Joseph', age=30, Height=6.3)
print('\nDictionary is Created Using the dict() Function:',dict_value)
print(type(dict_value))

 

In the above code, firstly a dictionary is created with three key-value pairs using the “curly braces {}” and then via the “dict()” function, respectively.

Output

The above output implies that the dictionary has been created successfully.

How to Access/Retrieve and Modify Dictionary Items in Python?

To retrieve a value in a dictionary, you can utilize the square brackets and provide the corresponding key name.

Example

The below-stated code is used to access and modify a dictionary value in Python:

dict_value = {'Name': 'Joseph', 'Age': 30, 'Height': 6.3}
# Accessing a value
print('Access Value of Dictionary is: ',dict_value['Name'])
# Modifying a value
dict_value['Age'] = 22
print('After Modification: ',dict_value)

 

In the above code, the specified dictionary value is accessed via the corresponding “key”. Also, another dictionary value is modified using the “square bracket”.

Output

In the above output, the dictionary value has been accessed and another value is modified accordingly.

Alternatively, we can retrieve a value by utilizing the “get()” method. This method returns “None” if the key doesn’t exist, which can be beneficial in specific situations. Here’s an example code:

dict_value = {'Name': 'Joseph', 'Age': 30, 'Height': 6.3}
# Using get() method
print(dict_value.get('Name'))
print(dict_value.get('Gender'))

 

According to the above code, the “get()” method is used to access the specified key from the dictionary. It is such that the former accessed key returns “Joseph” and the latter accessed key gives “None”.

Dictionary Methods and Operations

Python dictionaries come with a range of built-in methods and operations that make it easy to manipulate and work with them. Here are some of the most commonly used methods:

  • keys()”: retrieves a key list of the specified dictionary.
  • values()”: retrieves the values list of the specified dictionary.
  • items()”: retrieves a key-value pair list of the initialized dictionary.
  • update()”: Reconstructs/updates the dictionary with the key value from a different specified dictionary or iterable.
  • pop()”: deletes and retrieves the value for a given key.
  • clear()”: deletes all elements/items from the specified dictionary.

Example: Applying the Dictionary Methods

The below code provides the working of some of the commonly used dictionary methods:

dict_value = {'Name': 'Joseph', 'Age': 30, 'Height': 6.3}
# Using keys() method
print(dict_value.keys())
# Using values() method
print(dict_value.values())
# Using items() method
print(dict_value.items())
# Using update() method
dict_value.update({'Name': 'Alex'})
print(dict_value)
# Using pop() method
dict_value.pop('Age')
print(dict_value)
# Using clear() method
dict_value.clear()
print(dict_value)

 

In the above code, different dictionary methods are performed upon the initialized dictionary value “dict_value”.

Output

The above code snippet returns the corresponding functionalities of the applied dictionary methods.

Conclusion

Python “dictionaries” are a powerful and flexible data structure that can be utilized in a broad range of applications. They offer a range of built-in methods and operations that simplify their manipulation. This article demonstrated the working of dictionaries in Python.



from https://ift.tt/IFhqbfA

Post a Comment

0 Comments