Data Structures – Lists and Tuples in Python

A list is an ordered collection of arbitrary objects. If you or any of your students have programmed in Java or C, think about a list in Python as a list of objects. Unlike strings, individual elements of lists can be modified. In a more formal way, we say that lists in Python are mutable. Like strings, lists are indexed and indexing starts with 0.

Examples of basic Python list operations

##Creating new lists
myData = [ 4, 1, 8, 10, -2, 3 ]
yourData = [ 'Bill', 99, 42, 'Python']

##Retrieving an element from a list
print myData[3]   # prints 10
print myData[-1]  # prints 3
x = yourData[3]   # assigns yourData[3] to x
print x 		# prints Python

##Changing an element in a list
yourData[0] = 'Gates'
print yourData 	# prints ['Gates', 99, 42, 'Python']

# slicing
print yourData[1:4] # prints[99, 42, 'Python']

# repetition
yourData = yourData * 2
print yourData 	# ['Gates', 99, 42, 'Python', 'Gates', 99, 42, 'Python']

# concatenation
myData = myData + [1, -4]
print myData 		# [4, 1, 8, 10, -2, 3, 1, -4]

# appending an element to a list
myData.append(3)
print myData 		# [4, 1, 8, 10, -2, 3, 1, -4, 3]

# removing an element from a list
myData.remove(1) # removes the first item from the list whose value is x
print myData 		# [4, 8, 10, -2, 3, 1, -4, 3]

# summing up the elements in a list
total = 0
for i in myData:
    total += i
print total 		#total is 16



Python treats all information in programs as objects. Objects are always stored by reference. More information and examples about Classes & Objects and the List Class, can be found at Classes – Overview and Example 1: List.

The use of lists can simplify programs. One such example is the Monty Hall game described and used in the Conditional section.   Here is the same program written using lists.  Having students compare and discuss both versions of the programs can be informative and valuable for them.

Tuples

A tuple is a sequence of immutable Python objects. Tuples and lists are both sequences, but unlike a list, a tuple is immutable. This means one cannot modify the elements of a tuple. Creating a tuple is as simple as listing different comma-separated values. Alternatively, one can put these comma-separated values between parentheses.

Examples of basic Python tuple operations

##Creating new tuples
tp1 = 1, 2, 'Bill'
tp2 = (1, 2, 3, 4)
tp3 = ('Bill', 'Gates', 3)

##Retrieving an element from a tuple using its index
print tp1[0] 		# 1
print tp2[3] 		# 4
print tp3[1] 		# Gates

## concatenation
tp1 = tp2 + tp3
print tp1 # (1, 2, 3, 4, 'Bill', 'Gates', 3)

## slicing
print tp1[2:5]  # (3, 4, 'Bill')

##Creating a tuple with a single element is different
new_tp = (2,) 		# We have to include the final comma
print type(new_tp) 	# prints the type ‘tuple’

##Without the final comma, Python treats the (2) below as an integer in parentheses
tp = (2)
print type(tp) 	# It is an integer

##Tuples are immutable
##You cannot modify the elements of a tuple.
tp3[1] = 'Doors' # TypeError: 'tuple' object does not support item assignment