Python lists, dictionaries, tuples
List
Java list corresponds to the set, a list is composed of a plurality of values of the sequence. In the string, each character value is; in the list, the value can be any data type. Value in the list of referred element (Element) , sometimes referred to as item (Item) .
Create a list of
The easiest way is to use brackets ( [
and ]
) the elements together comprising:
[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']
The following list contains a string, a floating-point, integer and another list:
['spam', 2.0, 5, [10, 20]]
List contains no elements is called the empty list; you can use empty brackets []
create an empty list.
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [42, 123]
>>> empty = []
>>> print(cheeses, numbers, empty)
['Cheddar', 'Edam', 'Gouda'] [42, 123] []
The list is variable
And the string is different is that lists are mutable. When the bracket operator appears on the left side of an assignment statement, it points to an element in the list is assigned.
>>> numbers = [42, 123]
>>> numbers[1] = 5
>>> numbers
[42, 5]
in
Operators can also use the list.
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> 'Edam' in cheeses
True
>>> 'Brie' in cheeses
False
Traverse the list
The most common way is through the list using a for loop. The syntax and string traversal like this:
for cheese in cheeses:
print(cheese)
However, if you want to write or update elements in the list, you need to access through the index. One common approach is a combination of built-in functions range
and len
:
for i in range(len(numbers)):
numbers[i] = numbers[i] * 2
When the execution of an empty list for loop will not execute the main loop:
for x in []:
print('This never happens.')
List of operations
+ Operator splicing a plurality of lists:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> c
[1, 2, 3, 4, 5, 6]
* Operator to repeat a given number list:
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
List sliced
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']
If you omit the first index, the slice from the list head start. If you omit the second index, the slice will end to end of the list. So if you are both omitted, slice is a copy of the entire list.
Because the list is variable, usually before modifying the list, a copy of the list is very useful.
When the slice operator on the left side of an assignment statement can update multiple elements:
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3] = ['x', 'y']
>>> t
['a', 'x', 'y', 'd', 'e', 'f']
List method
Python provides methods of example, a list. append
Adding a new element to the end of the list:
>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> t
['a', 'b', 'c', 'd']
extend
We will accept a list as a parameter, and add it to a target where all the elements of the list:
>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
['a', 'b', 'c', 'd', 'e']
sort
The elements in the list are sorted from small to large
>>> t = ['d', 'c', 'e', 'b', 'a']
>>> t.sort()
>>> t
['a', 'b', 'c', 'd', 'e']