Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The Python3 list

The Python3 list

A sequence is the most basic data structure in Python.

Each value in a sequence has a corresponding position value called an index; the first index is 0, the second is 1, and so on.

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

Operations that can be performed on lists include indexing, slicing, addition, multiplication and membership testing.

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

A list is the most commonly used Python data type; it appears as comma-separated values inside square brackets.

The data items of a list need not have the same type

To create a list, simply enclose comma-separated data items in square brackets, as shown below:

list1

=

[

'Google'

,

'Runoob'

,

1997

,

2000

]

list2

=

[

1

,

2

,

3

,

4

,

5

]

list3

=

[

"a"

,

"b"

,

"c"

,

"d"

]

list4

=

[

'red'

,

'green'

,

'blue'

,

'yellow'

,

'white'

,

'black'

]


Accessing values in a list

As with string indices, list indices start at 0, the second index is 1, and so on.

Using indices, lists can be sliced, combined and so on.

Example

#!/usr/bin/python3

list

=

[

'red'

,

'green'

,

'blue'

,

'yellow'

,

'white'

,

'black'

]

print

(

list

[

0

]

)

print

(

list

[

1

]

)

print

(

list

[

2

]

)

The example above produces the following output:

red
green
blue

Indices can also start from the end: the last element has index -1, the one before it -2, and so on.

Example

#!/usr/bin/python3

list

=

[

'red'

,

'green'

,

'blue'

,

'yellow'

,

'white'

,

'black'

]

print

(

list

[

1

]

)

print

(

list

[

2

]

)

print

(

list

[

3

]

)

The example above produces the following output:

black
white
yellow

Use subscript indices to access values in a list; you can also use square brackets [] to slice characters, as shown below:

Example

#!/usr/bin/python3

nums

=

[

10

,

20

,

30

,

40

,

50

,

60

,

70

,

80

,

90

]

print

(

nums

[

0

:

4

]

)

The example above produces the following output:


[10, 20, 30, 40]

Slice using negative index values:

Example

#!/usr/bin/python3

list

=

[

'Google'

,

'Runoob'

,

"Zhihu"

,

"Taobao"

,

"Wiki"

]

Read the second element

print

(

"list[1]: "

,

list

[

1

]

)

Slice from the second element (inclusive) to the second-to-last element (exclusive)

print

(

"list[1:-2]: "

,

list

[

1

:-

2

]

)

The example above produces the following output:


list[1]:  Runoob
list[1:-2]:  ['Runoob', 'Zhihu']

Updating a list

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

Example (Python 3.0+)

#!/usr/bin/python3

list

=

[

'Google'

,

'Runoob'

,

1997

,

2000

]

print

(

"The third element is : "

,

list

[

2

]

)

list

[

2

]

=

2001

print

(

"The updated third element is : "

,

list

[

2

]

)

list1

=

[

'Google'

,

'Runoob'

,

'Taobao'

]

list1.

append

(

'Baidu'

)

print

(

"Updated list: "

,

list1

)

**Note:** We will discuss this in the following chapters append() how the method is used.

The example above produces the following output:


第三个元素为 :  1997
更新后的第三个元素为 :  2001
更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']

Deleting list elements

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

Example (Python 3.0+)

#!/usr/bin/python3

 

list

=

[

'Google'

,

'Runoob'

,

1997

,

2000

]

 

print

(

"Original list : "

,

list

)

del

list

[

2

]

print

(

"After deleting the third element : "

,

list

)

The example above produces the following output:


原始列表 :  ['Google', 'Runoob', 1997, 2000]
删除第三个元素 :  ['Google', 'Runoob', 2000]

**Note:** We will discuss the use of the remove() method in the following chapters


Python list script operators

The + and * operators on lists are similar to those on strings. + combines lists and * repeats a list.

As shown below:

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 an element exists in the list
for x in [1, 2, 3]: print(x, end=" ") 1 2 3 Iterate

Python list slicing and concatenation

List slicing in Python is similar to string operations, as shown below:

L

=

[

'Google'

,

'Runoob'

,

'Taobao'

]

operations:

Python expression Result Description
L[2] 'Taobao' Read the third element
L[-2] 'Runoob' Read the second-to-last element starting from the right: count from the right
L[1:] ['Runoob', 'Taobao'] Output all elements starting from the second element

L

=

[

'Google'

,

'Runoob'

,

'Taobao'

]

L

[

2

]

'Taobao'

L

[

2

]

'Runoob'

L

[

1

:

]

[

'Runoob'

,

'Taobao'

]

Lists also support concatenation:

squares

=

[

1

,

4

,

9

,

16

,

25

]

squares +

=

[

36

,

49

,

64

,

81

,

100

]

squares

[

1

,

4

,

9

,

16

,

25

,

36

,

49

,

64

,

81

,

100

]


Nested lists

Using nested lists means creating other lists inside a list, for example:

a

=

[

'a'

,

'b'

,

'c'

]

n

=

[

1

,

2

,

3

]

x

=

[

a

,

n

]

x

[

[

'a'

,

'b'

,

'c'

]

,

[

1

,

2

,

3

]

]

x

[

0

]

[

'a'

,

'b'

,

'c'

]

x

[

0

]

[

1

]

'b'


List comparison

Comparing lists requires the eq method from the operator module (see:Python operator Module):

Example

Import the operator module

import

operator

a

=

[

1

,

2

]

b

=

[

2

,

3

]

c

=

[

2

,

3

]

print

(

"operator.eq(a,b): "

,

operator

.

eq

(

a

,

b

)

)

print

(

"operator.eq(c,b): "

,

operator

.

eq

(

c

,

b

)

)

The output of the code above is:

operator.eq(a,b):  False
operator.eq(c,b):  True

Python list functions & methods

Python includes the following functions:

No. function
1 len(list)
Number of elements in the list
2 max(list)
Return the maximum value of the list elements
3 min(list)
Return the minimum value of the list elements
4 list(seq)
Convert a tuple into a list

Python includes the following methods:

No. Method
1 list.append(obj)
Add a new object at the end of the list
2 list.count(obj)
Count how many times an element appears in the list
3 list.extend(seq)
Append multiple values from another sequence at once at the end of the list (extend the original list with a new list)
4 list.index(obj)
Find the index of the first match of a value in the list
5 list.insert(index, obj)
Insert an object into the list
6 list.pop([index=-1])
Remove an element from the list (the last one by default) and return its value
7 list.remove(obj)
Remove the first match of a value in the list
8 list.reverse()
Reverse the elements in the list
9 list.sort( key=None, reverse=False)
Sort the original list
10 list.clear()
Clear the list
11 list.copy()
Copy a list