Computer Science

# Lists

• a lot like strings!
• a string is a sequence of characters
• a list is a sequence of — whatever you want!

## Creating a list

``````>>> letters = ['a', 'b', 'c', 'd']
>>> letters
['a', 'b', 'c', 'd']``````

### lists can hold a variety of things

``````letters = ['a', 'b', 'c', 'd']

ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']

scores = [100, 97, 83, 94, 99]

# empty list
nobody = []

# you can do this but it's not a good idea
mixed_list = ['goodbye', 83, 'p', 43.79, 'This is not a good idea!']
``````

## Length of a list

``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']

print(len(ways_to_say_goodbye))

nobody = []
print(len(nobody))``````
``````    5
0``````

## Indexing a list

``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']

print(ways_to_say_goodbye[3])``````
``    peace out``
``print(ways_to_say_goodbye[-1])``
``    ciao``
• finding the index
``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']
i = ways_to_say_goodbye.index('sayonara')
print(i)
j = ways_to_say_goodbye.index('whatever')
print(j)``````
``````    2

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

/var/folders/9x/cb134v3d2nb22_rksynbspqm0000gn/T/ipykernel_45336/2451658592.py in <module>
2 i = ways_to_say_goodbye.index('sayonara')
3 print(i)
----> 4 j = ways_to_say_goodbye.index('whatever')
5 print(j)

ValueError: 'whatever' is not in list``````

## Looping over a list

``````for item in ways_to_say_goodbye:
upper_item = item.upper()
print(upper_item)``````
``````    GOODBYE
LATER
SAYONARA
PEACE OUT
CIAO``````
``````for i in range(len(ways_to_say_goodbye)):
upper_item = ways_to_say_goodbye[i].upper()
print(upper_item)``````
``````    GOODBYE
LATER
SAYONARA
PEACE OUT
CIAO``````

## Lists are Mutable

• you can change them
• remember, strings are immutable (not changeable)
``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']

ways_to_say_goodbye[0] = 'see ya'
print(ways_to_say_goodbye)``````
``    ['see ya', 'later', 'sayonara', 'peace out', 'ciao']``
``````for i in range(len(ways_to_say_goodbye)):
ways_to_say_goodbye[i] = ways_to_say_goodbye[i].upper()
print(ways_to_say_goodbye)``````
``    ['SEE YA', 'LATER', 'SAYONARA', 'PEACE OUT', 'CIAO']``

## Appending to a list

• one way to change a list
``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']

ways_to_say_goodbye.append('see ya')
print(ways_to_say_goodbye)``````
``    ['goodbye', 'later', 'sayonara', 'peace out', 'ciao', 'see ya']``
``````ways_to_say_goodbye = []

ways_to_say_goodbye.append('goodbye')
ways_to_say_goodbye.append('later')
ways_to_say_goodbye.append('sayonara')

print(ways_to_say_goodbye)``````
``    ['goodbye', 'later', 'sayonara']``
• can append a lot in a loop
``````numbers = []
for i in range(10):
numbers.append(i * 10)
print(numbers)``````
``    [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]``
• the first example listed below does not work
• `append()` returns None
``````numbers = []
numbers = numbers.append(1)
print(numbers)

numbers = []
numbers.append(1)
print(numbers)``````
``````    None
[1]``````
• because lists are mutable, you change it directly
• different from strings
``````numbers = []
numbers.append(1)
print(numbers)

name = "Emma"
name = name + "!"
print(name)``````

### Example

• function that returns all the numbers from 0 to n - 1
``````def list_n(maximum):
numbers = []
for i in range(maximum):
numbers.append(i)
return numbers

some_numbers = list_n(10)
print(some_numbers)``````
``    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]``

## `in` and `not in` tests

``````letters = ['a', 'b', 'c', 'd']
if 'c' in letters:
print('c is in the list')``````
``    c is in the list``
``````if 'x' not in letters:
print('x is not in the list')``````
``    x is not in the list``

### Example

• create a new list that has all the items that are in both list `a` and list `b`
``````def intersect(a, b):
# accumulator pattern
result = []
# loop through all items in list a
for item in a:
# check if the item is in list b
if item in b:
result.append(item)
return result

intersection = intersect([1, 2, 3, 4, 5], [2, 3, 5])
print(intersection)
# order of lists doesn't matter
intersection = intersect([2, 3, 5], [1, 2, 3, 4, 5])
print(intersection)
``````
``````    [2, 3, 5]
[2, 3, 5]``````

### Example

• remember `index()`?
``````ways_to_say_goodbye = ['goodbye', 'later', 'sayonara', 'peace out', 'ciao']
# this creates an error
ways_to_say_goodbye.index('whatever')``````
• write a function called `find_index_of_item()`
• return the index for an item if it is in the list, otherwise return -1
``````def find_index_of_item(item, shopping_list):
if item in shopping_list:
return shopping_list.index(item)
return -1

my_shopping_list = ['sausage', 'peppers', 'tomatoes']
index = find_index_of_item('peppers', my_shopping_list)
print(index)
index2 = find_index_of_item('pasta', my_shopping_list)
print(index2)``````
``````    1
-1``````

# Constants

``````STATES = [ 'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA',
'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME',
'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM',
'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX',
'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY']

def find_the_best_state():
for state in STATES:
if state == 'OR':
print("Oregon is the best state")

find_the_best_state()
``````

Oregon is the best state

## Important notes on constants

• a type of “global” variable since it is not inside a function
• any function in your program can see it
• Python conventions
• upper case means its a constant
• it’s intended to be a read-only value, code should not modify it