Python List

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

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

The operations that can be performed on all sequences include indexing, slicing, adding, multiplying, and checking membership.

In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

Lists are the most commonly used Python data type and can appear as comma-separated values within square brackets.

The data items of a list do not need to be of the same type.

To create a list, just enclose different comma-separated data items in square brackets. As shown below:

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

Like string indexing, list indexing starts at 0. Lists can be sliced, combined, and so on.


Access values in a list

Use subscript indexing to access values in a list. Similarly, you can use square brackets to slice characters, as shown below:

Example (Python 2.0+)

#!/usr/bin/python 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]

The output of the above example is:

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

Update list

You can modify or update the data items of a list. You can also use the append() method to add list items, as shown below:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- list = [] ## Empty list list.append('Google') ## Use append() to add elements list.append('Example') print list

Note:We will discuss the use of the append() method in a later chapter.

The output of the above example is:

['Google', 'Example']

Delete list elements

You can use the del statement to delete list elements, as in the following example:

Example (Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1

The output of the above example is:

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

Note:We will discuss the use of the remove() method in a later chapter.


Python list script operators

Lists use the + and * operators in a similar way to strings. The + sign is used to combine lists, and the * sign is used to repeat lists.

As shown below:

Python expressionResultDescription
len([1, 2, 3])3Length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Combination
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition
3 in [1, 2, 3]TrueWhether the element exists in the list
for x in [1, 2, 3]: print x,1 2 3Iteration

Python list slicing

An example of Python list slicing is as follows:

>>>L = ['Google', 'Example', 'Taobao'] >>> L[2] 'Taobao' >>> L[-2] 'Example' >>> L[1:] ['Example', 'Taobao'] >>>

Description:

Python expressionResultDescription
L[2]'Taobao'Read the third element in the list
L[-2]'Example'Read the second-to-last element in the list
L[1:]['Example', 'Taobao']Slice the list starting from the second element

Python list functions & methods

Python includes the following functions:

No.Function
1cmp(list1, list2)
Compare elements of two lists
2len(list)
Number of list elements
3max(list)
Return the maximum value of list elements
4min(list)
Return the minimum value of list elements
5list(seq)
Convert a tuple to a list

Python includes the following methods:

No.Method
1list.append(obj)
Add a new object at the end of the list
2list.count(obj)
Count the number of times an element appears in the list
3list.extend(seq)
Append multiple values from another sequence to the end of the list at once (extend the original list with the new list)
4list.index(obj)
Find the index position of the first matching item of a value in the list
5list.insert(index, obj)
Insert an object into the list
6list.pop([index=-1])
Remove an element from the list (the last element by default) and return the value of that element
7list.remove(obj)
Remove the first matching item of a value in the list
8list.reverse()
Reverse the elements in the list
9list.sort(cmp=None, key=None, reverse=False)
Sort the original list
Other extensions