Intro to Python Programming (Part 3) - Python Lists
2021-05-19 | By Maker.io Staff
So far, you’ve learned what Python is and how you can create and compare new primitive variables. The second part of this series also discussed how to employ simple binary operations, such as addition and multiplication, to work with the values stored in variables. This article goes a step further and introduces you to a more advanced data structure - the Python list.
What are Python Lists?
The variables discussed so far could only hold a single value of any type. Remember that in Python, you don’t explicitly specify the data type of a variable. Instead, the variable’s value determines its type. You only supply the variable’s name and its value, and Python will work everything else out on its own.
It is, however, also possible to store multiple values in a single variable. You may know the concept of fixed-length arrays from other programming languages. Python, however, uses a different approach, and it doesn’t implement arrays the way other languages do. Instead, Python uses lists with variable lengths. A single list can contain not only a variable number of elements, but all those elements can also have completely different data types.
Note that many other programming languages also support dynamic-length lists. These lists, however, typically only work if all elements have the same underlying data type. You can define a list in Python using square brackets:
li = [1,2,3,4,5]
Doing so creates a new variable, called li, which represents a list. The list contains five elements (the five numbers written inside the square brackets). As mentioned, you can also mix the data types of the list elements as you like:
li = [-1, 37, “Element 1”, False, ‘c’, [16, True, -5, 8, False], 24]
The Basics of Working with Lists in Python
Now that you know what a Python list is and how to define one, it’s time to learn how to retrieve items from the list. Reading the value of an element in a list works exactly the same as reading the value of an in an array:
fruits = [“Apple”, “Banana”, “Kiwi”, “Papaya”, “Watermelon”] # Get the third element of the list (“Kiwi”) k = fruits[2]
To get the value of an element in the list, you use square brackets again and supply the index of the element you’re interested in. In this case, that’s the third element, which has index two. Note that indices start with zero in Python.
Using square brackets to read the value of an element will not remove that element from the list. You can use the pop() method to remove a value from a list. Doing so will also return the value before it’s removed from the list so that you can store it in a variable:
The pop() function is also great for removing elements from a list. To do so, call the pop() function without storing the returned value in a variable:
fruits.pop() # Deletes the last element of fruits fruits.pop(0) # Deletes the first element of fruits
Calling pop() on an empty list will cause the Python interpreter to throw an error. You can check the number of elements in a list with the len() function:
len(fruits)
You can also dynamically add elements to a list by using the append function:
Note that it is, again, up to you what elements you add to a list. You are free to mix the data types as you like.
The Python Array
If you only want to add numeric values to a list, it’s better to use a so-called Python array. These arrays are a specialized form of the previously discussed lists. They support all the previously discussed operations, but you cannot mix values of different data types. In a Python array, you can only use numeric values:
import array as python_array a = python_array.array('d', [1, 24.0, 3.14])
These arrays support the same operations as lists. They are, however, more optimized. You can read more about Python arrays in the official documentation.
Using Lists and Arrays in Your Projects
Lists are a collection of multiple values referenced by a single variable. In Python, lists represent arrays, and you can dynamically add and remove elements. Besides that, the list elements may have different data types. The Python array is an optimized list that can only hold numeric values. These different options provide the power of flexible definition and structure to many projects.
So far in this series, we’ve covered the basics of variables, the basics of Python interpreters, and now Python lists and arrays. In the next article, we’ll look at call functions and if-statements, looking at the utility of these logical steps in your projects.
Recommended Reading
- Intro to Python Programming (Part 1) - Getting Started
- Intro to Python Programming (Part 2) - Python Interpreters
- Intro to Python Programming (Part 4) - Call Functions and If-Statements
- Intro to Python for Makers (Part 5) - Custom Functions
- Intro to Python for Makers (Part 6) - Classes and Objects
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum