4. Lists#

Today, we’ll introduce lists, our first data structure!

But first, let’s recap:

4.1. Recap#

Variables

You can think of a variable as a box in storage that contains a thing (or things). The name of the variable is a label on the box. When you use a variable in code, you get the value out of the box.

A variable can hold almost anything in Python. But there are a few simple data types that will cover most of the data we work with:

  • Numeric - ints and floats

  • String - any textual data, denoted by quotes (’’ or “”). Any character can be put into a string.

  • Boolean - True or False

Functions

Functions are our actors; they perform actions.

Python is a stickler for grammar, so be careful to follow the syntax for a function EXACTLY! (this applies to all structures in Python) demo functions

4.2. Lists#

(Read WTOP Ch 6)

A list is an ordered grouping of data. So if a variable is a box, a list is a box of boxes!

  • enclosed in brackets with items separated by commas [A, B, C, D]

  • can contain any object type and types don’t need to be consistent within the list

A = [1,2, 'hello', 3.5, False]

print(A)
[1, 2, 'hello', 3.5, False]

4.2.1. Adding to lists#

You can add A SINGLE item to the end of a list with .append(item). However, this item could be a list. ‘Append’ updates the list in place.

You can concatenate two lists with +.

A.append('celery')
print(A)
[1, 2, 'hello', 3.5, False, 'celery']
B = ['DS', 256]
print(A+B)
print(A)
print(B)
[1, 2, 'hello', 3.5, False, 'celery', 'DS', 256]
[1, 2, 'hello', 3.5, False, 'celery']
['DS', 256]
A.append(B)
print(A)
[1, 2, 'hello', 3.5, False, 'celery', ['DS', 256]]

4.2.2. Length of lists#

You can get the length of a list with the function len(list)

len(A)
7

4.2.3. Indexing of lists#

The locations in a list are indexed by number, starting at 0. That is, a list starts with a zero-th entry.

Alternatively, you can index lists from the end backwards starting with -1 (the last item in a list).

indices

new_list = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'theta']
print(len(new_list))
7
new_list[-1]
'theta'

You extract a value from a list using brackets. For example, myList[5] would return the 6th entry of the list.

And you can use indexing to assign new values to items in the list.

print(new_list[5])
print(new_list[-2])
zeta
zeta

4.2.4. Slicing#

A slice is an extraction of a sub-list from a list.

You slice a list as:

new_list[startIdx:stopIdx:stepSize]

Notes:

  • the list will go up to but not include the stopIdx.

  • any of the parameters can be skipped.

Try these! First, guess what these slices will do. Then type them into your notes to check:

  • new_list[0:5:2]

  • new_list[0:5]

  • new_list[4::]

  • new_list[:3]

  • new_list[-3:]

new_list = [‘alpha’, ‘beta’, ‘gamma’, ‘delta’, ‘epsilon’, ‘zeta’, ‘theta]

new_list = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'theta']
new_list[-1::-1]
['theta', 'zeta', 'epsilon', 'delta', 'gamma', 'beta', 'alpha']
['theta', 'zeta', 'epsilon', 'delta', 'gamma', 'beta', 'alpha']
['theta', 'zeta', 'epsilon', 'delta', 'gamma', 'beta', 'alpha']

4.2.4.1. Exercises#

Write a slice to select the colored blocks. There’s more than one way to solve each of these problems.

1. q1

new_list[1:5]
['beta', 'gamma', 'delta', 'epsilon']

2.

q2

new_list[3]
'delta'

3.

q3

new_list[1::2]
['beta', 'delta', 'zeta']

4.

q4

new_list[-2:]
['zeta', 'theta']

5.

q5

new_list[3:]
['delta', 'epsilon', 'zeta', 'theta']

6.

q6

new_list[:4]
['alpha', 'beta', 'gamma', 'delta']

4.2.5. Finding an element in a list#

If a list contains an element, you can find where that element is using .index(searchItem, startIdx, endIdx). index will only return the first instance it finds in the specified range.

my_favorite_pets = ['Ramona RIP', 'Boyd', 'MJ', 'Raylan', 'Puff', 'MJ']
my_favorite_pets.index('MJ', 3)
5

4.3. Tuples#

Tuples are just like lists EXCEPT:

  • They are immutable, meaning once they are created you can not change them.

  • They are identified by parantheses () instead of brackets [].

When functions return multiple values, those values are stored as a tuple by default.

my_tuple = (1,2,'hello')
my_eplut = my_tuple[-1::-1]
print(my_eplut)
('hello', 2, 1)
new_list[3] = 'D'
print(new_list)
['alpha', 'beta', 'gamma', 'D', 'epsilon', 'zeta', 'theta']