Modal Title
Data Science / Software Development

Python for Beginners: Lists

How to work with lists in Python.
Dec 31st, 2021 9:00am by
Featued image for: Python for Beginners: Lists

Our Python for beginners tutorial continues, so let the celebratory “Huzzahs!” ring out. We first introduced to you what makes Python so special, then we introduced variables, learned how to accept input from users, and discovered how to save the input to a file.

We’re going to continue building on those tutorials and doing so in such a way that anyone can follow. Our goal is to help those who aren’t programmers by nature to learn a language and feel they’ve grasped it enough to make it work for them.

This time around, we’re going to talk about lists. We use lists all the time. Shopping lists, to-do lists… you know the drill. But a list within Python is a big of a different beast… sort of.

A list in Python is used to store multiple items for a single variable. You remember variables:


In the above example, we’ve assigned the value “green” to the variable “color.” We can then use that variable in our script like so:


When we run our program, it would print out:


Simple. But what if we wanted to define multiple colors? Sure, we could always do something like this:


To print out those colors, we could do something like this:


Our script would look like:


You probably won’t be surprised to find out we could do that even easier with the help of a list. Let’s stick with our colors. Let’s define the color variable with a list of four colors. That variable declaration would look like this:


Now, here’s something you have to understand. You might think the position of each list item would be blue = 1, green = 2, red = 3, and yellow = 4. It’s important to understand that, within the realm of software development, counting starts at 0. So our color positions would be blue = 0, green = 1, red = 2, and yellow = 3. Why is this important? Let me show you.

Say you want to print out the color green. To do that, you would add the line:


So our application would look like this:


When you run that program, it would print out the color green.

Let’s add to this. Say you want to define a fruit variable to coincide with the colors. So we could define:


You see where this is going. To print out green apple, you’d use the following statement:


We’ve printed the color value at position 1 and the fruit value at position 1.

Pretty nifty.

What if we wanted to map all the colors to all the fruits? That would look something like this:


That script would look like:


If we run that program (using a command like python lists.py), the output would look like this:


Let’s get a bit tricky here. Instead of a single line of output, what if we wanted our output to look like this:


To do that, we need to take a leap forward in our Python skills and introduce a framework, called Pandas. Pandas is a framework for data analysis and manipulation. To make use of Pandas, it has to be installed. I’m demonstrating on Ubuntu Linux 20.04 and the installation of Pandas is done with the command:

sudo apt-get install python3-pandas -y

Once the framework is installed, you have it at your disposal. To use Pandas we have to import it into our program with the line:

import pandas as pd

What that means is we’ll call the Pandas functions in our scripts using pd. The Pandas function we’re going to work is DataFrame, which allows you to work with two-dimensional, size-mutable, potentially heterogeneous tabular data. In other words, it makes it possible for you to more easily work with different types of data.

Still with me?


So far, so good. Now, we get to the challenging part. The first thing we’re going to do is define a new variable using our color and fruit variables as columns with the DataFrame function. This new variable will be called df. Remember, we’re calling Pandas with pd and the function we’re using is DataFrame. That line looks like this:


So our variable is df, we’re calling Pandas with pd, we’re using the DataFrame function and formatting the variables color and fruit as columns.

Good? Good.

Now, we’re going to define the labels for our columns using the variables we’ve already created with the line:


Finally, we print it all out with:


Our entire program looks like this:


Now, when we run the application, we’ll see the output as we expected:


We did take a jump in complexity there, you now know how to make use of external frameworks (such as Pandas), to make Python even more useful and flexible.

You should already feel smarter.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.