TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python / Software Development

Python for Beginners: Lists

A programming tutorial for 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:

color = "blue"

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:

print (color)

When we run our program, it would print out:

blue

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

color1 = "blue"
color2 = "green"
color3 = "red"
color4 = "yellow"

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

print (color1,color2,color3,color4)

Our script would look like:

#Define our variables as colors
color1 = "blue"
color2 = "green"
color3 = "red"
color4 = "yellow"

#Print out a list of colors
print (color1,color2,color3,color4)

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:

color = ['blue','green','red','yellow']

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:

print (color[1])

So our application would look like this:

#Define our variable as a list
color = ['blue','green','red','yellow']

#Print out a color
print (color[1])

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:

fruit = ['blueberry','apple','cherry','banana]

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

print (color[1],fruit[1])

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:

print (color[0],fruit[0], color[1], fruit[1], color[2], fruit[2], color[3], fruit[3])

That script would look like:

#Define our color variable as a list
color = ['blue','green','red','yellow']
#Define our fruit variable as a list
fruit = ['blueberry','apple','cherry','banana]

#Print out both variables
print (color[0],fruit[0], color[1], fruit[1], color[2], fruit[2], color[3], fruit[3])

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

blue blueberry green apple red cherry yellow banana

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

0   color    fruit
1   blue     blueberry
2   green    apple
3   red      cherry
3   yellow   banana

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, the parts of our script you now understand look like this:

#Import the Pandas framework, defined as pd
import pandas as pd

#Define our color variable as a list
color = ['blue','green','red','yellow']
#Define our fruit variable as a list
fruit = ['blueberry','apple','cherry','banana']

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:

df=pd.DataFrame(columns=['color', 'fruit'])

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:

df['COLOR'],df['FRUIT']=color,fruit

Finally, we print it all out with:

print(df)

Our entire program looks like this:

#Import the Pandas framework, defined as pd
import pandas as pd

#Define our color variable as a list
color = ['blue','green','red','yellow']
#Define our fruit variable as a list
fruit = ['blueberry','apple','cherry','banana']

#Define the df variable as formatted columns for color and fruit
df=pd.DataFrame(columns=['color', 'fruit'])
#Label our columns as color and fruit
df['color'],df['fruit']=color,fruit

#Print everything
print(df)

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

0   color    fruit
1   blue     blueberry
2   green    apple
3   red      cherry
3   yellow   banana

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.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.