Python lists and tuples#

  • List is the most versatile Python data type to group values with others

  • Can be written as a list of comma-separated values (items) between square brackets.

  • Tuples are written between parenthesis. They are read-only lists.

  • Lists can contain items of different types.

  • Like strings, lists can be indexed and sliced.

  • Lists also support operations like concatenation.

Indexing#

squares = [1, 4, 9, 16, 25]
print(squares)
[1, 4, 9, 16, 25]
print(squares[0])  # indexing returns the item
1
squares[-1]
25
squares[-3:] # slicing returns a new list
[9, 16, 25]
squares += [36, 49, 64, 81, 100]
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • Unlike strings, which are immutable, lists are a mutable type.

cubes = [1, 8, 27, 65, 125]  # something's wrong here  
cubes[3] = 64  # replace the wrong value, the cube of 4 is 64, not 65!
print(cubes)
[1, 8, 27, 64, 125]
cubes.append(216)  # add the cube of 6
print(cubes)
[1, 8, 27, 64, 125, 216]
cubes.remove(1)
print(cubes)
[8, 27, 64, 125, 216]

Assignment#

  • You can change the size of the list or clear it entirely.

  • The built-in function len() returns list size.

  • It is possible to create lists containing other lists.

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters[2:5] = ['C', 'D', 'E'] # replace some values
print(letters)
['a', 'b', 'C', 'D', 'E', 'f', 'g']
letters[2:5] = [] # now remove them
print(letters)
['a', 'b', 'f', 'g']
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
x
[['a', 'b', 'c'], [1, 2, 3]]
x[0]
['a', 'b', 'c']
x[0][1], len(x)
('b', 2)

Assignment, Copy and Reference#

a = [0, 1, 2, 3, 4]
b = a
print("b = ",b)
b =  [0, 1, 2, 3, 4]
b[1] = 20        # Change one value in b
print("a = ",a) # Y
a =  [0, 20, 2, 3, 4]

b is a reference to a, they occupy same space memory

b = a[:] # assign a slice of a and you create a new list
b[2] = 10
print("b = ",b)
print("a = ",a)   
b =  [0, 20, 10, 3, 4]
a =  [0, 20, 2, 3, 4]

Some useful List Methods#

a = list("Python-2020")
a
['P', 'y', 't', 'h', 'o', 'n', '-', '2', '0', '2', '0']
a.sort()
a
['-', '0', '0', '2', '2', 'P', 'h', 'n', 'o', 't', 'y']
a.reverse()
a
['y', 't', 'o', 'n', 'h', 'P', '2', '2', '0', '0', '-']
a.pop() #pop the last item and remove it from the list
a
['y', 't', 'o', 'n', 'h', 'P', '2', '2', '0', '0']

Dictionary#

They are indexed by keys, which are often strings.

person = dict(firstname="John", lastname="Smith", email="john.doe@domain.fr")
person['size'] = 1.80
person['weight'] = 70
person
{'firstname': 'John',
 'lastname': 'Smith',
 'email': 'john.doe@domain.fr',
 'size': 1.8,
 'weight': 70}
print(person.keys())
dict_keys(['firstname', 'lastname', 'email', 'size', 'weight'])
print(person.items())
dict_items([('firstname', 'John'), ('lastname', 'Smith'), ('email', 'john.doe@domain.fr'), ('size', 1.8), ('weight', 70)])

Exercises#

  • Split the string “python ENSAI 2020” into the list [“python”,”ENSAI”, 2020]

  • Insert “september” and value 7 before 2020 in the result list.

  • Capitalize the first item to “Python”

  • Create a dictionary with following keys (meeting, month, day, year)

  • Print out the items.

  • Append the key “place” to this dictionary and set the value to “ENSAI”.

['python', 'ENSAI', '2020']
['python', 'ENSAI', 'september', 7, '2020']
['Python', 'ENSAI', 'september', 7, '2020']
{'course': 'Python','september': 'september','day': 7,'year': '2020','place': 'ENSAI'}