Introduction to Lists in Python

Updated on 28 Dec 2022

Lists are a simple sequence of related values. Consider the following code

student1 = 'brent'
student2 = 'paul'
student3 = 'andrea'

Now consider the sort of operations that we might want to perform. Capitilize the first letter, make sure they are all alpha characters, print the value. Unfortunately we have 3 variables (and probably more) that we’d have to repeat the operations for. It would be better if we could do one operation on all the data… lists are a way to help us.

Introduction

We’ve already been using lists in our previous code, and it looks like this.

students = ['brent', 'paul', 'andrea']

And we already know how to iterate over each item of a list

students = ['brent', 'paul', 'andrea']

for student in students:
    #do some stuff
    pass

List - basic operations

The python documentation shows a few operations that would be useful for us when dealing with lists. The main ones we’ll focus on are:

  • len number of elements in the list
  • in check if an element is in the list
students = ['brent', 'paul', 'andrea']
num_elements = len(students)

print('there are {0} elements in the list'.format(num_elements))

We can also check to see if a ‘value’ is in a list.

students = ['brent', 'paul', 'andrea']
num_elements = len(students)

if 'andrea' in students:
    print('The name you are looking for is in the list!')

List - slicing operations

Remember when we were doing variables / strings and we encountered the split method. Lists have something similar, but before we get to that did you know we can get specific elements? We did that as part of the myFile.pdf exercise.

students = ['brent', 'paul', 'andrea']
first = students[0]

print('The first student is {0}'.format(first))

Subset

We can also get a subset of the list.

students = ['brent', 'paul', 'andrea']
subset = students[1:3]

print(subset)

The : operator has the following rules

  • [start_index:end_index] - get a subset from start_index upto but not including end_index
  • [:end_index] - get a subset from the beginning upto but not including end_index
  • [start_index:] - get a subset from start_index to the end
  • [index] - get element at the index

Index value

There might be cases where we want to use or know the sequence number. We can do that using the enumerate built-in method.

students = ['brent', 'paul', 'andrea']

for i, v in enumerate(students):
    print('student_' + str(i) + ' - ' + v)

Lists - guided exercise

Let’s assume that we have 2 synchronized lists.

students = ['brent', 'paul', 'andrea']
grades   = ['High Distinction', 'Pass', 'Credit']

How do we get the following output?

Solution

A for v in students: approach may not necessarily work because we also need the corresponding grades element, and therein lies the clue. students[index] => grades[index], i.e. the index is the same between boths lists! With this revelation there are several approaches that we can now take; lets look at one.

students = ['brent', 'paul', 'andrea']
grades   = ['High Distinction', 'Pass', 'Credit']

for i, v in enumerate(students):
    print(v + ' has a grade of ' + grades[i])

Lists - extended exercise

Another approach is to use len() and while loops with an index counter. Utilize your earlier knowledge of these concepts to rework the above solution.