Python Tutorial_ a Tutorial Lists
Python Tutorial_ a Tutorial Lists
Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact
Lists
>>> L = [3, 4]
>>> L += [42]
>>> L
[3, 4, 42]
Data Protection
We will compare in the following example the different approaches and calculate their run times. To understand the following program, you need to know that time.time() returns a float number, the time in seconds
Declaration
since the so-called ,,The Epoch''1. time.time() - start_time calculates the time in seconds consumed for the for loops:
Data Protection
import time Declaration
n= 100000
start_time = time.time()
l = []
for i in range(n):
l = l + [i * 2]
print(time.time() - start_time)
start_time = time.time()
l = []
for i in range(n):
l += [i * 2]
print(time.time() - start_time)
start_time = time.time()
l = []
for i in range(n):
l.append(i * 2)
print(time.time() - start_time)
26.3175041676
0.0305399894714
0.0207479000092
We can see that the "+" operator is about 1268 slower than the append method. The explanation is easy: If we use the append method, we will simply append a further element to the list in each loop pass. Now we
come to the first loop, in which we use l = l + [i * 2]. The list will be copied in every loop pass. The new element will be added to the copy of the list and result will be reassigned to the variable l. After this the old
list will have to be removed by Python, because it is not referenced anymore. We can also see that the version with the augmented assignment ("+="), the loop in the middle, is only slightly slower than the version
using "append".
It is possible to remove with the method "remove" a certain value from a list without knowing the position.
s.remove(x)
This call will remove the first occurrence of x from the list s. If x is not contained in the list, a ValueError will be raised. We will call the remove method three times in the following example to remove the colour
"green". As the colour "green" occurrs only twice in the list, we get a ValueError at the third time:
The method "index" can be used to find the position of an element within a list:
It returns the first index of the value x. A ValueError will be raised, if the value is not present. If the optional parameter i is given, the search will start at the index i. If j is also given, the search will stop at position
j.
insert
We have learned that we can put an element to the end of a list by using the method "append". To work efficiently with a list, we need also a way to add elements to arbitrary positions inside of a list. This can be
done with the method "insert":
s.insert(index, object)
An object "object" will be included in the list "s". "object" will be placed before the element s[index]. s[index] will be "object" and all the other elements will be moved one to the right.
The functionality of the method "append" can be simulated with insert in the following way:
Footnotes:
1 Epoch time (also known as Unix time or POSIX time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1
January 1970, not counting leap seconds.
© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein