Python Tuples and Dictionaries

List elements can be modified throughout the execution of a program because it is a mutable data structure.

Tuples

Tuple elements cannot be modified. It is sometimes called an immutable list except that it is indicated by parentheses instead of square braces.

tuples and dictionaries

Elements in a tuple can be accessed individually by their indexes. Let’s create a tuple and access some of its elements.

tuple

As said earlier, elements in a tuple cannot be modified. Let’s see what happens when we try to replace an element in our previously created tuple.

modify tuple error

We can't modify elements in a tuple but we can reassign the variable that represents the tuple.

tuple

Dictionaries

Dictionaries can be used to model various real world situations. Imagine you are to build a dating app/website. You can represent each user’s information with a dictionary. Dictionaries are indicated by curly braces ({ }).

Let’s create a dictionary for a new user on the website.

dictionary

Elements in a dictionary are represented as key-value pairs. The keys are connected to their respective values.

The keys have to be of immutable data types like String, Float or Boolean while the values can be of any data type. Even dictionaries can be used as values.

The keys in our previously created dictionary are name, eye_color and location while the values are Smith, blue and Denver

Accessing Dictionary Values

Dictionary values can be accessed through their respective keys. The dictionary name should be followed by the key placed in square braces.

Let’s access the values in our dictionary

dictionary values

Let’s print a message to the users whenever the log on to the website.

f string with dictionaries

Notice how we used single quotes in double quotes to avoid errors.

Adding New Dictionary Elements

New dictionary elements are added by assigning a value to a new key. The dictionary name should be followed by the key in square braces, an equals symbol (=) and the value.

Let’s add a new attribute to the preexisting user, user_1.

add dictionary element

As seen from the output, a new attribute, height, has been added for the user.

Replacing Dictionary Values

To replace a value in a dictionary, the dictionary name should be followed by a key enclosed in square braces then a new value is assigned to it

Let’s replace the value associated to the location of user_1.

replace dictionary item

The location was modified successfully as seen from the output.

Removing Dictionary Elements

To delete an element from a dictionary, the del keyword should be followed by the dictionary name and the key of the element. An element in a dictionary is a key-value pair.

Let’s delete the eye_color attribute for user_1.

remove dictionary item

The output shows that the attribute has been deleted successfully. Deleted elements are removed permanently.

Accessing Dictionary Values with get Method

Trying to access the value of a key that does not exists will throw an error.

Imagine trying to access the value associated to the age attribute from user_1 dictionary

key error

Errors break codes. Anything after the line of code that tries to access the value of the age attribute would not execute.

The get() method returns a default value, None, whenever a key that doesn’t exist in a dictionary is accessed. The key is passed as an argument to this method

get method

A customized message can also be specified in the get() method. The message is passed as a second argument to the method

get method

The get() method allows your program to continue even if the age attribute does not exist. Although, lines of code that need the age value after this line might throw errors except being handled by conditional statements.

Accessing all Dictionary Keys and Values

All the keys in a dictionary can be accessed through the keys() method. The keys are represented with a list.

Similarly, all the values in a dictionary can be accessed through the values() method. The values are also represented with a list.

dictionary keys and values

Nesting

List of Dictionaries

Dictionaries can be used in dynamic ways to model various use cases. We can nest dictionaries in lists to represent all the users of our app/website where each dictionary represents a user.

Let’s create a list to represent the users and append the preexisting user to it.

list of dictionaries

Let’s now create a new user, user_2, and append it to our list.

list of dictionaries

We can access the name of the first user by the following syntax

access dictionary element

Dictionary of Dictionaries

We can also have a dictionary of dictionaries where each dictionary represents a user’s information.

Let’s create an empty dictionary and add the users to it.

dictionary of dictionaries

We can access the name of user_2 by the following syntax

access dictionary element

Your choice of nesting depends on the nature of your project.

Summary

  • Tuple elements cannot be modified
  • Dictionary elements are represented as key-value pairs
  • Dictionaries can be nested in lists or other dictionaries to model various real world situations

No comments:

Powered by Blogger.