Lists and Tuples
Sequences
Like Strings, Python has two other common sequence types: Lists and Tuples. While lists and tuples are similar to strings in that they share many of the same functionality, they are unique in that they are used to group other values together. Both lists and tuples define a number of values separated by commas, but lists are enclosed in square brackets [ ] and are mutable, meaning their contents can change, while tuples are enclosed in parentheses ( ) and are immutable, meaning their contents can't change.
Lists
As stated above, lists are groups of comma separated values enclosed in square brackets, but can utilize many of the features available to other sequences like strings.
# Lists are very simple to create.
myList = [1, 2, 3]
print myList # Will print out: [1, 2, 3]
# Empty lists can also be created, to have items added to them later.
myList = []
# Lists are not confined to hold values of a single data type either.
myList = [1, "hello", 3.3]
# Lists can even hold other lists! In this case, myList would actually hold 5 elements:
# 1, a list, 4, 'that', and another list. The last list also contains a list as well.
myList = [1, ["this", 3.3], 4, 'that', [6, [7.7, 'other'], 9]]
List Concatenation
Like strings, lists also support concatenation and can be combined to form a single larger list.
a = [1, 2, 3]
b = ['four', 'five', 'six']
print a + b # Will print out [1, 2, 3, 'four', 'five', 'six']
List Indexing
Like strings, lists are also indexed, which allows you to grab single values or splice them to get ranges of values. Just like everything else in Python, lists start with 0.
myList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print myList[3] # Will print out: d
print myList[-2] # Will print out: f
print myList[2:5] # Will print out: ['c', 'd', 'e']
When trying to index a nested list, we simply need to add a second index after the first.
myList = [1, 2, [3, [4, 5, 6, 7], 8], 9, 10]
print myList[2][1][3] # Will print out: 7
Appending to Lists
You can add values directly to the end of a list with the append() function. You can add anything that is normally allowed in a list.
myList = []
myList.append('Hello')
myList.append('World')
print myList # Will print out ['Hello', 'World]
List Functions
Below is a list of common list functions. Some of them are similar to other sequences like strings, while others are unique to lists because lists are mutable.
Function | Description | Example | Output |
---|---|---|---|
len(list) | Returns the length of the list. |
|
|
x in list | Will return True if x is within the list, False if not. Can also be used to iterate through the list. |
|
|
list.index(x) | Will return the index number of item x. Throws an error if item is not found. |
|
|
min(list) | Returns the smallest item of list. |
|
|
max(list) | Returns the largest item of list. |
|
|
list.count(x) | Will return the number of times x appears in the list. |
|
|
list.append(x) | Will add x to the end of the list. |
|
|
list.insert(i, x) | Will insert x at position i. |
|
|
list.remove(x) | Will remove the first x from the list. |
|
|
list.pop([i]) | Will remove the item at index i, and return it. If no index is specified, it will remove and return the last item in the list. |
|
|
list.reverse() | Will reverse the items in the list. |
|
|
Tuples
Tuples look similar to lists in that they are defined as a group of comma separated values, but they are enclosed by parenthesis and they are immutable like strings, meaning they can't be altered. Besides that, they are sequences, like lists and strings. This means that tuples have the functionality that other sequences have, such as concatenation, indexing and slicing, and even nesting.
# Tuples are very simple to create.
myTuple = (1, 2, 3)
print myTuple # Will print out: (1, 2, 3)
# Empty tuples can also be created, to have items added to them later.
myTuple = ()
# Like lists, tuples are not confined to hold values of a single data type either.
myTuple = (1, "two", 3.3)
# Tuples can even hold other tuples!
myTuple = (1, ("two", 3.3), 4, 'five', (6, (7.7, 'eight'), 9))
a = (1, 2, 3)
b = (4, 5, 6)
# Combine two tuples to make a new tuple
print a + b # Will print out: (1, 2, 3, 4, 5, 6)
myTuple = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
print myTuple[2:5] # Will print out: ('c', 'd', 'e')
Lists and tuples can also be nested within each other!
Tuple Functions
All of the functions that work on tuples work on lists, so these should look familiar. However, because they are immutable, not all of the list functions work on tuples. Check out the common tuple functions below.
Function | Description | Example | Output |
---|---|---|---|
len(tuple) | Returns the length of the tuple. |
|
|
x in tuple | Will return True if x is within the tuple, False if not.Can also be used to iterate through the tuple. |
|
|
min(tuple) | Returns the smallest item of tuple. |
|
|
max(tuple) | Returns the largest item of tuple. |
|
|