Python Dictionary

1. What is a Python dictionary

  • A Python dictionary is a built-in data structure that stores a collection of key-value pairs. It is an unordered collection, meaning that the elements in a dictionary are not stored in any particular order. Instead, each element is accessed and retrieved by its associated key.
  • Dictionaries are incredibly versatile and commonly used in Python programming due to their efficiency in searching and retrieving values. The keys in a dictionary must be unique and immutable objects, such as strings, numbers, or tuples. On the other hand, the values can be of any data type and can be mutable.
  • One of the main advantages of dictionaries is their ability to provide fast lookups. Rather than iterating over the elements to find a specific value, dictionaries use a hashing mechanism to directly map keys to their corresponding values. This makes dictionary lookups very efficient, even for large collections of data.
  • Dictionaries can be created using curly braces ({}) or by using the built-in dict() function. Key-value pairs are separated by colons (:), and multiple pairs are separated by commas. Once a dictionary is created, elements can be accessed, added, modified, or removed using various methods and operations.
  • Python dictionaries also offer several useful methods for manipulating and retrieving data. For example, the keys() method returns a list of all the keys in the dictionary, while the values() method returns a list of all the values. The items() method returns a list of tuples containing the key-value pairs.
  • Dictionaries can be iterated over using loops, providing access to each key or value in the collection. Additionally, Python provides convenient ways to check for the existence of a key in a dictionary and to retrieve a default value if the key is not present.
  • Due to their flexibility and efficient lookup capabilities, dictionaries are commonly used for tasks such as storing and retrieving configurations, building data mappings, counting occurrences of elements, and much more. They are a powerful tool in the Python language that enables efficient data manipulation and organization.

Syntax

To access a value from the dictionary, simply use the name of the dictionary followed by the corresponding key in square brackets:

Example

Example: phone book

2. Browse the values ​​and keys of a Python dictionary

A Python dictionary has a method called values() that allows you to browse its values, and another named keys() to browse its keys.

Example: print values ​​from a dictionary

Example: browse dictionary keys

Note

You can also browse keys and values ​​at the same time by using the items() method

Example: browsing through keys and values




3. Update Python dictionary: add or delete elements

3.1 Update a dictionary element

You can update a dictionary item directly by assigning a value to a key:

Example: Stock Manager

3.2 Add an element to the dictionary

In the case of a non-existent key, the same method mentioned above, allows to add elements to the dictionary:

Example: Add an item to the stock

3.3 Delete an element from the dictionary

You can remove an element from the dictionary by specifying its key in the pop() method

Example: deleting a dictionary element

Note

A dictionary has another method: popitem() that removes the last element

Example: Deleting the last item

3.4 Emptying a dictionary

A Python dictionary can be empty using the clear() method

Example: empty a dictionary

4. Associated methods with a Python dictionary

Here is a summary of the main methods associated with a dictionary object:

  1. clear(): removes all items from the dictionary.
  2. copy(): returns a shallow copy of the dictionary.
  3. fromkeys (seq [, v]): Returns a new dictionary with the keys of seq and a value equal to v (the default is None).
  4. get(key [, d]): returns the value of key. If the key does not exit, returns d (the default is None).
  5. items(): returns a new view of dictionary items (key, value).
  6. keys(): Returns a new view of the dictionary keys.
  7. pop(key [, d]): removes the element with key and returns its value or d if key is not found. If d is not provided and the key can not be found, raises KeyError.
  8. popitem(): delete and return an arbitrary element (key, value). Lift KeyError if the dictionary is empty.
  9. setdefault(key [, d]): if key is in the dictionary, return its value. Otherwise, insert the key with the value d and return d (the default is None).
  10. update([other]): Update the dictionary with the key / value pairs of other existing keys.
  11. values(): returns a new view of dictionary values

 

Younes Derfoufi
CRMEF OUJDA

Leave a Reply