Python Tutorial Part 3 | Data Structures: Lists

This section will dive into the world of data structures used within the Python programming environment, starting with one of the more commonly used structures called ‘lists.’

Previously, we discussed various data types that contain information from text to numerical values, critical for every computer system, including PLCs, IPCs, and more advanced OS platforms.

What are Data Structures?

In contrast to data types, data structures are objects used in Python to store and organize collections of individual data items. They differ from data types in their ability to store groups of data versus a single entity. These structures have different characteristics and modes of interacting with the objects stored within them. The four most common types of data structures in Python are Lists, Dictionaries, Tuples, and Sets.

In this article, we will explain the first of those examples: lists.

What is a List?

The list data structure in Python is more commonly referred to as an array structure in other programming languages. There are a myriad of resources online giving textual definitions of what a list is. So rather than repeat common literature, let’s use a manufacturing process example to better suit our controls engineering domain. Let’s take an arbitrary chemical process “Jet Fuel Mercaptan Oxidation Treating” as illustrated below.

Process flow diagram

Figure 1. Chemical process flow used to relate concepts of a Python list. 

In our chemical process, there are numerous individual equipment entities analogous to variables of data types. These include the pump, pipewash, reactors, and beds. While each of these elements is singular, it is the collection or union of them within the process that is of importance. An engineer could not obtain the desired outcome of sweetened jet fuel with a single reactor. The list data structure, much like the process flow diagram, groups these individual entities together for the importance of the domain application. The analogous Python list appears as below:

Note the list is assigned to a variable of the chemical process name and is invoked by square brackets [ ]. For now, each equipment entity is of a string data type, but lists can contain mixed data types (which we will touch upon more later).

Order of Items

Another reason why the list is a great data structure to represent the chemical process above is the importance of equipment order. If, for example, the clay bed is placed at the start of the chemical process instead of the end, it would destroy the process and lead to an undesired chemical product. Thus, the order or position of the equipment in the process is also critical.

Lists in Python retain the order or placement of elements within them by using an index. The index is a numeric indication of an element’s place in the list. We can access the equipment position by calling the list variable with square brackets containing the index value like below:

The first item is denoted by position 0 which may conceptually seem odd. However, much like in many PLCs, Python is a 0-based indexing system which is a fancy way of saying the first element is denoted by a 0 instead of a 1. The return in PyCharm is below:

Numeric index value

Figure 2. Using the numeric index value to access equipment specified at that place in the list. 

Mutability

Mutability is a complex way of saying that individual items within a list can be changed. Continuing with our example, let’s say an engineer wanted to rename the salt bed to indicate a more specific name such as “Sodium Chloride Bed”. One way to accomplish this is by reassigning the index value of the “Salt Bed” item to “Sodium Chloride Bed”:

The output of the fifth equipment element (remember, this is item #4 starting at #0) in our list now appears as below:

Renaming elements

Figure 3. Renaming an equipment element in the list. 

List Methods

What if the engineer wanted to do more than just rename an existing piece of equipment? Well, this could be accomplished with methods specific to the List data structure. Lists, like everything else in Python, are objects. As such, objects can have specific methods or functions that perform operations on instances of the objects. Lists, being a built-in data structure native to Python, have pre-built methods that can be used to accomplish some of these common tasks of mutability.

List methods in Python

Figure 4. Visual of built-in list methods available in Python. 

Method 1:.append()

In my experience, this is the method I use most commonly. It adds a single item to the end of the list. Let’s add a random number to the end of our chemical process list. Remember how lists can contain multiple data types?

The output appears:

Method 2:.extend()

Accomplishes the same purpose as append(), but can be used to add multiple items to the end of a list at once. For example, let’s add multiple strings from one list to our jet fuel list:

The output appears:

Method 3:.remove()

This method can be used to remove the first occurrence of the item in the list scanning from left to right. Let’s say we wanted to remove the “water wash” equipment element from the jet fuel list:

The output appears:

Wrapping Up

We’ll continue to touch upon the other data structures in Python in further (forthcoming) sections. This section covers one of the most commonly used data structures in the language.


Copyright Statement: The content of this website is intended for personal learning purposes only. If it infringes upon your copyright, please contact us for removal. Email: [email protected]

Leave a Comment