Lists Review

Sequences

What's a sequence, and what are some operations and built-in functions that sequences support?

A sequence is an ordered collection of elements. Sequences support operations like:

Sequences Continued

We know two sequence data types. What are they?

Lists

What's a list? What elements are allowed in a list?

List Syntax

In code, what's one way to create a list (as in, using a list literal), and how is an empty list represented?

"""
a list is delimited by brackets, with each value separated by a comma
"""
items = ["some", "stuff", "between", "brackets"]

"""
an empty list is two square brackets - open and close
"""
an_empty_list = []

Indexing and Slicing Operations

How do we index into and slice out substrings of a list?

>>> items = ['foo', 'bar', 'baz', 'qux']
>>> items[0]
'foo'
>>> items[1:3]
['bar', 'baz']
>>> 

About Indexes

Speaking of indexes, what type should an index be (in both indexing and slicing)?

>>> """ indexes are integers """
>>> items = [True, False, False]
>>> items[0]
True
>>> items[0.0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not float

Indexing

Given a list items, what are three ways to retrieve the last element (operations, functions and methods are valid)?

>>> items = [True, False, False]
>>> items[-1]
False
>>> items[len(items) - 1]
False
>>> items.pop()
False

An Aside on Pop

By the way, name the two theings that pop does when called on a list.

Speaking of Mutability

What will happen when the code below is executed? Is anything printed out? Is there an error? Does nothing happen?

items = [1, 2, 3]
items[0] = "boo!"
print(items)

List of Lists

Using the same list:

stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
stuff[1][1]
stuff[2][2] = 'something'

Every Item in a List

How can I continuously retrieve every item in a list, one item at a time?

""" use a for loop """
for number in [24, 48, 12]:
	print "%s more hours to go!" % number

For Loops

What is the value of the loop variable, number, during each iteration?

for number in [24, 48, 12]:
	print "%s more hours to go!" % (number)

Nested Lists

How do for loops work with nested lists? What does this print out and what type is another_list in each iteration?

stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
for another_list in stuff:
	print another_list
""" another list is always a list in this case! """
[9, 16, 25]
['a', 'b', 'c']
[[], "", None]

Nested Lists Continued

With the same list, how do I print out every element of every list on its own line with an exclamation point?

stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
for inner_list in stuff:
	for item in inner_list:
		print "%s!" % (item)

Slicing

What will the following slices return? →

numbers = [5, 11, 17, 19]
print(numbers[0:3])
print(numbers[2:])
print(numbers[:2])
print(numbers[0:100])
[5, 11, 17]
[17, 19]
[5, 11]
[5, 11, 17, 19]

Slicing Returns a New Sub List, It Does Not Alter the Original List!

What will the following slices print? →

nonsense = ['foo', 'bar', 'baz', 'qux']
print(nonsense[1:3])
print(nonsense)
['bar', 'baz']
['foo', 'bar', 'baz', 'qux']

Comparison Operators

What will the following code output?

a = [1, 2, 3]
b = [1, 2, 3]
c = [1, 6, 3]
d = [1, 6, 3, 2]

print(a != b)
print(b > c)
print(d > c)
False
False
True

Addition and Multiplication

What will the following code output?

a = [1, 2, 3]
b = ['x', 'y', 'z']
print(a + b)
print(a * 3)
[1, 2, 3, 'x', 'y', 'z']
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Length, Deletion and Membership

What operators and/or functions would I use to (and how would I use them):

Length, Deletion and Membership Continued

>>> nonsense = ['foo', 'bar', 'baz', 'qux']

>>> len(nonsense)
4
>>> del nonsense[0] 
>>> print(nonsense)
['bar', 'baz', 'qux']
>>> del nonsense[20]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> 'baz' in nonsense
True

List are (Mutable) Objects

Lists are objects, and they have a slew of methods that you can call on them. However, because lists are mutable, many methods actually change the list in place! Let's see how this differs from strings:

>>> s = 'hello'
>>> s.upper()
'HELLO'
>>> print(s)
hello
>>> numbers = [5, 3, 4]
>>> numbers.sort()
>>> print(numbers)
[3, 4, 5]

Lists vs Strings

Name as many differences between lists and strings as you can!

Back to Mutability

Because lists are mutable, how do list methods typically work? Do they return values? Change the original object?

Adding Elements

Name three methods that add elements to a list. What does each method do? What are each method's inputs and return value?

Removing Elements

Name two methods that delete elements from a list. What does each method do? What are each method's inputs and return value?

Miscellaneous Methods

Are there any other list methods that we know of? What does each method do? What are each method's inputs and return value?

List Exercises