Skip to main content
Version: 8.1

Lists and Tuples

Inductive University

Basic Python - Lists and Dictionaries

Watch the video

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.

Python - Lists
# 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.

Python - List Concatenation
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.

Python - List Indexing
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.

Python - Index a Nested List
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.

Python - Appending to 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.

FunctionDescriptionExampleOutput
len(list)Returns the length of the list.
list = [1, 2, 3, 4, 5] 
print len(list)
5
x in listWill return True if x is within the list, False if not. Can also be used to iterate through the list.
list = [1, 2, 3, 4, 5] 

if 4 in list:
print "There is a 4 in the list"

for value in list:
print value
There is a 4 in the list 
1
2
3
4
5
list.index(x)Will return the index number of item x. Throws an error if item is not found.
list = [1, 2, 3, 4, 5] 

print list.index(3)
2
min(list)Returns the smallest item of list.
list = [1, 2, 3, 4, 5]

print min(list)
1
max(list)Returns the largest item of list.
list = [1, 2, 3, 4, 5] 

print max(list)
5
list.count(x)Will return the number of times x appears in the list.
list = [1, 2, 3, 4, 5, 4]

print list.count(4)
2
list.append(x)Will add x to the end of the list.
list = [1, 2, 3, 4, 5]

list.append(6)

print list
[1, 2, 3, 4, 5, 6]
list.insert(i, x)Will insert x at position i.
list = [1, 2, 3, 4, 5]

list.insert(1, 5)

print list
[1, 5, 2, 3, 4, 5] 
list.remove(x)Will remove the first x from the list.
list = [1, 2, 3, 2, 4, 5]

list.remove(2)

print list
[1, 3, 2, 4, 5] 
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 = [1, 2, 3, 4, 5]

list.pop(2)

print list

list.pop()

print list
3

[1, 2, 4, 5]

5

[1, 2, 4]
list.reverse()Will reverse the items in the list.
list = [1, 2, 3, 4, 5]

list.reverse()

print list
[5, 4, 3, 2, 1] 

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.

Python - Tuples
# 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')
note

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.

FunctionDescriptionExampleOutput
len(tuple)Returns the length of the tuple.
tuple = [1, 2, 3, 4, 5]

print len(tuple)
5
x in tupleWill return True if x is within the tuple, False if not.

Can also be used to iterate through the tuple.
tuple = [1, 2, 3, 4, 5]

if 4 in tuple:
print "There is a 4 in the tuple"

for value in tuple:
print value
There is a 4 in the tuple
1
2
3
4
5
min(tuple)Returns the smallest item of tuple.
tuple = [1, 2, 3, 4, 5]

print min(tuple)
1
max(tuple)Returns the largest item of tuple.
tuple = [1, 2, 3, 4, 5]

print max(tuple)
5