4.1. Lists - Python Notes (0.14.0)
4.1. Lists - Python Notes (0.14.0)
0)
4.1. Lists
SEARCH
Contents
Lists Go
Quick example Enter search terms
Difference between append() and extend() or a module, class or
Other list methods function name.
Operators This page: Show
Slicing source.
List comprehension TABLE OF
Filtering Lists CONTENTS
Lists as Stacks 1. Quick Start /
Lists as Queues Tutorial
How to copy a list
2. Variables,
In python, lists are part of the standard language. You expressions,
will find them everywhere. Like almost everything in statements,
Python, lists are objects. There are many methods types
associated to them. Some of which are presented here 1. Iterators
below.
2. Generators
4. Decorators
>>> l = [1, 2, 3]
>>> l[0] 5. Classes
1
>>> l.append(1) 6. Namespace and
>>> l
scoping rules
[1, 2, 3, 1]
7. Packaging
https://thomas-cokelaer.info/tutorials/python/lists.html 1/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
>>> stack
['a', 'b', 'c']
given list rather the list itself. You can do that manually or a module, class or
2. Generators
>>> my_list = ['a','b','c','b', 'a']
>>> my_list.index('b')
3. Exceptions
1
9. Notes about
4.1.3.2. insert
booleans and
You can remove element but also insert element
logical operators
wherever you want in a list:
10. Notes on
>>> my_list.insert(2, 'a')
>>> my_list
['b', 'c', 'a', 'b']
https://thomas-cokelaer.info/tutorials/python/lists.html 2/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
4.1.3.3. remove
Similarly, you can remove the first occurence of an
element as follows:
>>> my_list.remove('a')
>>> my_list
['b', 'c', 'b', 'a']
SEARCH
4.1.3.4. pop
Go
Or remove the last element of a list by using: Enter search terms
Imagine now that you have some non-standard types. 6. Namespace and
You can overwrite the function used to perform the scoping rules
comparison as the first argument of the sort() method.
7. Packaging
There is also the possiblity to sort in the reverse order:
8. Notes about
>>> my_list.sort(reverse=True) sorting lists and
>>> my_list
dictionaries
['c', 'b', 'b', 'a']
9. Notes about
booleans and
4.1.3.7. reverse
logical operators
Finally, you can reverse the element in-place:
10. Notes on
>>> my_list = ['a', 'c' ,'b']
>>> my_list.reverse()
>>> my_list
['b', 'c', 'a']
https://thomas-cokelaer.info/tutorials/python/lists.html 3/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
4.1.4. Operators
The + operator can be used to extend a list:
9. Notes about
4.1.6. List comprehension booleans and
logical operators
Traditionally, a piece of code that loops over a
sequence could be written as follows: 10. Notes on
>>> evens = []
>>> for i in range(10):
... if i % 2 == 0:
... evens.append(i)
https://thomas-cokelaer.info/tutorials/python/lists.html 4/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
>>> evens
[0, 2, 4, 6, 8]
https://thomas-cokelaer.info/tutorials/python/lists.html 5/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
>>> l2 = list(l)
>>> l2 = l[:]
>>> import copy
>>> l2 = copy.copy(l)
SEARCH
Warning
Don’t do l2 = l, which is a reference, not a copy. Go
Enter search terms
The preceding techniques for copying a list create or a module, class or
shallow copies. IT means that nested objects will not be function name.
copied. Consider this example: This page: Show
source.
>>> a = [1, 2, [3, 4]]
>>> b = a[:] TABLE OF
>>> a[2][0] = 10 CONTENTS
>>> a
[1, 2, [10, 4]] 1. Quick Start /
>>> b Tutorial
[1, 2, [10, 4]]
2. Variables,
To get around this problem, you must perform a deep
expressions,
copy:
statements,
>>> import copy types
>>> a = [1, 2, [3, 4]]
>>> b = copy.deepcopy(a) 1. Iterators
>>> a[2][0] = 10
>>> a 2. Generators
[1, 2, [10, 4]]
>>> b 3. Exceptions
[1, 2, [3, 4]]
4. Decorators
5. Classes
4.1.10.1. Inserting items into a sorted list
The bisect module provides tools to manipulate sorted 6. Namespace and
lists. scoping rules
>>> x = [4, 1]
7. Packaging
>>> x.sort()
>>> import bisect
8. Notes about
>>> bisect.insort(x, 2) sorting lists and
>>> x dictionaries
[1, 2, 4]
9. Notes about
To know where the index where the value would have
booleans and
been inserted, you could have use:
logical operators
>>> x = [4, 1]
>>> x.sort() 10. Notes on
>>> import bisect
>>> bisect.bisect(x, 2)
2
https://thomas-cokelaer.info/tutorials/python/lists.html 6/7
2021/5/13 4.1. Lists — Python Notes (0.14.0)
SEARCH
Go
Enter search terms
or a module, class or
function name.
2. Variables,
expressions,
statements,
types
1. Iterators
2. Generators
3. Exceptions
4. Decorators
5. Classes
6. Namespace and
scoping rules
7. Packaging
8. Notes about
sorting lists and
dictionaries
9. Notes about
booleans and
logical operators
10. Notes on
https://thomas-cokelaer.info/tutorials/python/lists.html 7/7