Python 3 Lists
Python 3 Lists
Sequences are the most basic data structure in Python. Each element in a sequence is assigned a number—its position, or index. The first index is 0, the second is 1, and so on.
Python has six built-in sequence types, but the most common are lists and tuples.
Operations on sequences include indexing, slicing, addition, multiplication, and checking membership.
In addition, Python has built-in methods for determining the length of a sequence and the largest and smallest elements.
A list is the most commonly used Python data type and appears as a comma-separated list of values enclosed in square brackets.
List items do not need to be of the same type
To create a list, simply enclose the comma-separated items in square brackets. As shown below:
list1 = ['Google', 'Geekdoc', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
Like string indices, list indices start at 0. Lists can be truncated, combined, and so on.
Accessing List Values
Use subscript indexing to access list values. You can also use square brackets to delimit characters, as shown below:
Example (Python 3.0+)
#!/usr/bin/python3
list1 = ['Google', 'Geekdoc', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])
Run Example »
The above example outputs:
list1[0]: Google
list2[1:5]: [2, 3, 4, 5]
Update List
You can modify or update the data items in a list, or you can use the append() method to add list items, as shown below:
Example (Python 3.0+)
#!/usr/bin/python3
list = ['Google', 'Geekdoc', 1997, 2000]
print ("The third element is: ", list[2])
list[2] = 2001
print ("The updated third element is: ", list[2])
Note: We will discuss the use of the append() method in the next chapter.
The output of the above example is:
The third element is: 1997
The updated third element is: 2001
Delete list elements
You can use del Statement to delete elements of a list, as shown in the following example:
Example (Python 3.0+)
#!/usr/bin/python3
list = ['Google', 'Geekdoc', 1997, 2000]
print ("Original list: ", list)
del list[2]
print ("Delete the third element: ", list)
The above example outputs:
Original list: ['Google', 'Geekdoc', 1997, 2000]
Delete the third element: ['Google', 'Geekdoc', 2000]
Note: We will discuss remove() in the next section. Method Usage
Python List Script Operators
List operators (+ and *) are similar to string operators. The + operator is used to combine lists, and the * operator is used to repeat 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 | Is the element in the list? |
for x in [1, 2, 3]: print(x, end=” “) | 1 2 3 | Iteration |
Python list truncation and concatenation
Python list truncation and string operation types are as follows:
L=['Google', 'Geekdoc', 'Taobao']
Operation:
Python expression | Result | Description |
---|---|---|
L[2] | ‘Taobao’ | Read the third element |
L[-2] | ‘Geekdoc’ | Read the second to last element from the right: count from the right |
L[1:] | [‘Geekdoc’, ‘Taobao’] | Output all elements starting from the second element |
>>>L=['Google', 'Geekdoc', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'Geekdoc'
>>> L[1:]
['Geekdoc', 'Taobao']
>>>
Lists also support concatenation operations:
>>>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 within lists, 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'
Python List Functions & Methods
Python includes the following functions:
Sequence Number | |
---|---|
1 | len(list) Number of elements in a list |
2 | max(list) Returns the maximum value of a list |
3 | min(list) Returns the minimum value of a list |
4 | list(seq) Converts a tuple to a list |
Python includes the following methods:
Sequence number | |
---|---|
1 | list.append(obj) Adds a new object to the end of a list |
2 | list.count(obj) Counts the number of times an element appears in a list |
3 | list.extend(seq) Appends multiple values from another sequence to the end of a list (extends the original list with the new list) |
4 | list.index(obj) Finds the index of the first occurrence of a value in a list |
5 | list.insert(index, obj) Insert an object into a list |
6 | list.pop([index=-1]) Remove an element from a list (default is the last element) and return the value of that element |
7 | list.remove(obj) Remove the first occurrence of a value from a list |
8 | list.reverse() Reverse the elements in a list |
9 | list.sort( key=None, reverse=False) Sort the original list |
10 | list.clear() Clear the list |
11 | list.copy() Copy the list |