Functions on list
append(x) Add an item to the end of the list.
a[len(a)] = x
extend(L) Extend a list by appending all the items of given list
L
a[len(a):] = L
insert(index, x) Insert
x
beforea[index]
1 2 |
|
remove(x) Remove all values
x
from the list. If there is no valuex
, there will be an errorpop(index)
Remove the item at the given position index
in the list and return it.
But you can use pop()
to return and remove the last item in the list
clear() Remove all items from the list
index(x) Return the index of value
x
in the list. There will be an error if no such item.count(x) Return the nuber of times value
x
appears in the listsort() Sort the items of the list in place
reverse() Reverse the items of list in place
copy() Return a copy of the list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Every method has return value.
insert
,remove
andsort
will returnNone
Using Lists as Stacks
Use append()
and pop()
to implement First in first out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Using Lists as Queues
To implement First in Last out, use collections.deque
1 2 3 4 5 6 7 8 9 10 |
|
List Comprehensions
You can do
1 2 3 4 5 6 |
|
or
1
|
|
Another Example:
1 2 |
|
equivalent to
1 2 3 4 5 6 7 |
|
More Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Nested List Comprehension
Implement a 3x4 matrix
1 2 3 4 5 |
|
The following will switch the row and column.
1 2 |
|
This code is also equal to
1 2 3 4 5 6 |
|
The built-in function
zip()
can provide the same functionality.
1 2 |
|
The del statement
Remove an item from a given list by a index.
1 2 3 4 5 6 7 |
|
Delete an entire list
1 2 3 |
|
Tuples and Sequences
The tuples is always enclosed in parentheses. Nested tuple is correct. The elements in Tuples can not be changed.
1 2 3 4 5 6 7 8 9 |
|
You can build empty tuple and comma is not considered when you calculate the length of tuple
1 2 3 4 5 6 7 |
|
Sets
- No duplicate elements
- Elements are unordered
To create an empty set, you have to use
set()
. If you do{}
, you will create an empty dictionary.
1 2 3 |
|
- Set support fast membership testing
1 2 3 4 |
|
- Set support
-
,|
,&
,^
operations
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Dictionaries
- Indexed by unique Keys. Key must be
immutable
type. List cannot be used as key. - Check if a key is in the dictionary by using the
in
keyword - List all keys by
list(d.keys())
orsorted(d.keys())
- Each element in dictionary is
key:value
separated by comma
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
dict()
can build dictionaries from sequences of key-value pairs:
1 2 |
|
You can create dictionaries with expressions
1 2 |
|
If your keys are string, you can also do this
1 2 |
|
Looping
- For dictionaries, Retrieve the key and value at the same time by using
items()
1 2 3 4 5 |
|
- For a sequence, the position index and corresponding value can be retrieved at the same time using
enumerate()
function
1 2 3 4 5 |
|
- To loop over two or more sequences at the same time, entries can be paired with the
zip()
function.
1 2 3 4 5 6 7 |
|
- To loop over a sequence in reverse, call
reversed()
function
1 2 3 4 5 6 7 |
|
Sorted()
function can loop over a sequence in sorted order and return a new sorted list while leaving source unaltered.
1 2 3 4 5 6 7 |
|
Conditinos
is
andis not
compare whether two objects are the same.in
andnot in
check whether a value occur in a sequence.- Comparision can be chained
1
|
|
- If you compare two sequence object with same type. It will use
lexicographical
ordering to compare them. The longer, the larger.
1 2 3 4 5 6 |
|