Latest web development tutorials

Python list (List)

Python sequence is the most basic data structure. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has a built-in types 6 sequence, but the most common are lists and tuples.

Sequence of operations can be carried out, including indexing, slicing, add, multiply, check the members.

Moreover, Python has a built-determine the length of the sequence and determining the maximum and minimum element method.

Python is a list of the most commonly used type of data, it can be used as a comma-separated values ​​appear in square brackets.

List of data items need not have the same type

Create a list as long as the comma-delimited data items using different brackets can be. As follows:

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

With the index of the string as a list of index starts from 0. List can be intercepted, combinations and the like.

Access list value

Use subscripting to access values ​​in the list, you can also use square brackets in the form of interception of character, as follows:


list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Examples of the above output:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

update list

You can modify the list of data items or updates, you can also use append () method to add a list of items, as follows:

list = ['physics', 'chemistry', 1997, 2000];

print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];

We will discuss the use of the next chapter in the append () method: Note

Examples of the above output:

Value available at index 2 :
New value available at index 2 :

Remove list element

You can use the del statement to remove elements of the list, the following examples:


list1 = ['physics', 'chemistry', 1997, 2000];

print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;

Examples of the above output:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

We will discuss the use of the next chapter in the remove () method: Note

Python script list operator

List of + and * operators and string similarity. + Sign for the combined list, an asterisk for the repeat list.

As follows:

Python expression result description
len ([1, 2, 3]) 3 length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] combination
[ 'Hi!'] * 4 [ 'Hi!', 'Hi!', 'Hi!', 'Hi!'] repeat
3 in [1, 2, 3] True Whether the elements are present in the list
for x in [1, 2, 3]: print x, 123 Iteration

Python list interception

Python list interception string type of operation, as follows:

L = ['spam', 'Spam', 'SPAM!']


Python expression result description
L [2] 'SPAM!' Read a list of the third element
L [-2] 'Spam' Read a list of the inverse of the second element
L [1:] [ 'Spam', 'SPAM!'] From the beginning of the second element interception list

Python list of functions & methods

Python includes the following functions:

No. function
1 cmp (list1, list2)
Compare the two lists of elements
2 len (list)
The number of list elements
3 max (list)
Back to the list of elements Max
4 min (list)
Returns a list of the minimum elements
5 list (seq)
Will be converted to a list of tuples

Python includes the following methods:

No. method
1 list.append (obj)
In the end of the list to add new objects
2 list.count (obj)
Number of times an element statistics appear in the list
3 list.extend (seq)
Multiple values ​​at the end of the list of additional disposable another sequence (extension of the original list with a new list)
4 list.index (obj)
Find the index position of the first occurrence of a value from a list
5 list.insert (index, obj)
Insert objects into a list
6 list.pop (obj = list [-1] )
Remove one element in the list (by default the last element), and returns the value of the element
7 list.remove (obj)
Remove the list a value of the first match
8 list.reverse ()
Reverse list element
9 list.sort ([func])
The original list is sorted