Lists and Tuples
Contents
Lists and Tuples#
So far we have covered the following data types:
int
,float
, andbool
. Now we will coverlist
, andtuple
Data types in Python can be classified as
mutable
andimmutable
.Immutable data types are:
int
,float
,bool
, andtuple
Mutable data types are:
list
,dict
, andset
NOTE: In other programming language, data types such as ints and floats are passed to a function using
pass-by-value
and data types such as arrays arepass-by-reference
. This means that changes made to ints and floats inside a function do not change the value outside of the function because they were passed a copy (independent copy). As for arrays, they are passed to a function as a reference, so changes made to the array persist outside the function.NOTE: In Python, however, everything is passed to a function using
pass-by-object-reference
.
Warning
Ref: Python for Programmers by Paul Deitel and Harvey Deitel
In many programming languages there are two ways to pass arguments–pass-by-value and pass-by-reference (sometimes called call-by-value and call-by-reference, respectively):
With pass-by-value, the called function receives a copy of the argument’s value and works exclusively with that copy. Changes to the function’s copy do not affect the original variable’s value in the caller.
With pass-by-reference, the called function can access the argument’s value in the caller directly and modify the value if it’s mutable.
Python arguments are ALWAYS passed by reference. Some people call this pass-by-object-reference, because “everything in Python is an object.” When a function call provides an argument, Python copies the argument object’s reference–not the object itself–into the corresponding parameter. This is important for performance. Function often manipulate large objects–frequency copying them would consume large amount of computer memory and significantly slow down program performance.
List#
list
is a container for storing a sequence of values. The values can be of different type. When the values are allint
,float
, orbool
, you can use special functions.
List Index – starts at 0#
whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3]
Creating a list#
grades = ['A', 'B', 'C', 'D', 'F']
Accessing a list#
grades[0]
Slicing a list#
grades[2:4]
grades[1::2]
grades[-1]
grades[::-1]
Reassigning a list#
grades = ['A', 'B', 'C', 'D', 'F']
grades[0] = 'a'
grades[1:2] = 'a'
grades[2:] = ['d', 'f']
Deleting from a list#
grades = ['A', 'B', 'C', 'D', 'F']
del grades[0]
del grades[1:3]
del grades
Concatenate lists#
grades1 = ['A', 'B', 'C']
grades2 = ['D', 'F']
grades = grades1 + grades2
Multiplication#
grades = ['A', 'B', 'C', 'D', 'F']
grades *= 3
Can store different data types#
But you will lose some processing functionality
my_list = ['A', 1, 'Spam', True]
my_list2 = [['John', [55, 65, 86]], ['Jane', [70, 80, 80]]
Built-in List methods#
len()
calculate length of listmax()
calculate max of listmin()
calculate min of listsum()
calculate sum of listsorted()
return a sorted listlist()
cast to type list – convert tuple to list or a generator to listany()
returnTrue
if the truthiness of any value isTrue
in the listall()
returnTrue
if the truthiness of all the values isTrue
in the list
numbers = [3, 4, 8, 9, 5, 6, 7, 0, 1, 2, 10, 11, 12]
len(numbers)
max(numbers)
min(numbers)
sum(numbers)
sorted(numbers)
list(numbers)
any(numbers)
all(numbers)
booleans = [True, False, True]
any(booleans)
all(booleans)
booleans = [True, True, True]
any(booleans)
all(booleans)
List methods (functions)#
append()
- add an element to end of listinsert()
- insert an element to the list at the specified locationremove()
- remove the elementpop()
- remove the last element in the list; also returns the value; you can save this to another variableclear()
- empty the listindex()
- return position of first matching elementcount()
- return the number of elements in the listsort()
- sort the list in placereverse()
- reverse the list in place
grades = ['A', 'B', 'C']
grades.append('D')
grades.insert(4, 'F')
grades.remove(2)
grades.pop()
grades.index('C')
grades.count() # len(grades)
grades.sort()
grades.reverse()
List unpacking#
a, b = [3,4]
Tuples#
Tuple are just like lists except that they are immutable. Once you have created a tuple, you cannot modify it.
Why use them?
Faster than lists
Make code safer – because you cannot change it
Valid keys in a dictionary
Writing to a file#
with open('test_file.txt', 'w') as file:
file.write('line1\nline2')
title = '|' + '{:^51}'.format('Cereal Yields (kg/ha)') + '|'
line = '+' + '-'*15 + '+' + ('-'*8 + '+')*4
row = '| {:<13} |' + ' {:6,d} |'*4
header = '| {:^13s} |'.format('Country') + (' {:^6d} |'*4).format(1980, 1990,
2000, 2010)
file_content = '\n'.join(('+' + '-'*(len(title)-2) + '+',
title,
line,
header,
line,
row.format('China', 2937, 4321, 4752, 5527),
row.format('Germany', 4225, 5411, 6453, 6718),
row.format('United States', 3772, 4755, 5854, 6988),
line))
with open('test_file.txt', 'w') as file:
file.write(file_content)