Python List#
In Python, a list is an ordered and mutable collection of data.
A list can contain items of various data types, and the items can be modified, added, or deleted.
Let’s assume we need to handle 10,000 values.
Creating 10,000 variables would be highly inefficient.
Therefore, Python provides lists to efficiently handle multiple values.
1. Creating a List#
A list can be created using [ ] as follows.
You can place multiple values inside the [ ] to handle them as a single list.
In the following example, integers, floats, and strings are all included in a single list.
An important feature is that a single list can contain multiple data types.
Each value contained in a list is called an element.
v = [1, 2.0, 'abc']2. Read#
You can read values from a list’s elements.
In print( ), v[0] means to read the value at index 0 from list v, which is 10.
(Indexes start from 0, not 1.)
In print( ), v[1] means to read the value at index 1 from list v, which is 20.
v = [10, 20]
print(v[0])
print(v[1])3. Write#
You can write values to a list’s elements.
List v contains [10, 20], and then the value at index 0 has been written to 99.
When you use print( ), you can see that the value of v[0] has been changed to 99.
v = [10, 20]
v[0] = 99
print(v[0])
print(v[1])4. List Slicing#
You can easily split elements using list slicing syntax.
It’s a very convenient syntax, so be sure to learn it.
In the following code, v[0:5] means to read from index 0 to index 4.
Therefore, [‘a’, ‘b’, ‘c’, ’d’, ’e’] is printed.
※ Even though the code is v[0:5], you must clearly recognize that it reads up to index 4, not index 5.
v = ['a', 'b', 'c', 'd', 'e']
print(v[0:5])Let’s look at the following similar example.
Since it’s v[0:3], it will read indexes 0, 1, and 2.
Therefore, [‘a’, ‘b’, ‘c’] is printed.
v = ['a', 'b', 'c', 'd', 'e']
print(v[0:3])In the following code, v[0: ] does not specify the ending index for slicing.
In this case, the slice automatically extends to the last element of the list, so the result of print( ) is [‘a’, ‘b’, ‘c’, ’d’, ’e’].
v = ['a', 'b', 'c', 'd', 'e']
w = v[0: ]
print(w)In the following code, v[0:99] specifies an ending index for slicing that is larger than the length of the list.
In this case as well, the slice is automatically capped at the last element of the list and does not raise an error.
v = ['a', 'b', 'c', 'd', 'e']
w = v[0:99]
print(w)In the following code, v[0:5:1] means to read from index 0 to index 4, stepping by 1.
Therefore, the result of print( ) is [‘a’, ‘b’, ‘c’, ’d’, ’e’].
v = ['a', 'b', 'c', 'd', 'e']
w = v[0:5:1]
print(w)In the following code, v[0:5:2] means to read from index 0 to index 4, stepping by 2.
Therefore, the result of print( ) is [‘a’, ‘c’, ’e’].
In [arg1:arg2:arg3], arg3 specifies the step size, and if omitted, it defaults to 1.
v = ['a', 'b', 'c', 'd', 'e']
w = v[0:5:2]
print(w)If you do not specify the start and end index for slicing, as in v[ : :1], it reads all the elements of the list.
If the start index is not specified, it automatically defaults to the first element of the list.
If the end index is not specified, it automatically defaults to the last element of the list.
v = ['a', 'b', 'c', 'd', 'e']
w = v[ : :1]
print(w)v[ : :-1] means to read the list in reverse order, stepping by 1.
Therefore, the result of print( ) is [’e’, ’d’, ‘c’, ‘b’, ‘a’], displaying the list in reverse order.
v = ['a', 'b', 'c', 'd', 'e']
w = v[ : :-1]
print(w)As in the following code, if you write v[ : :-2], it reads in reverse order stepping by 2, so the result is [’e’, ‘c’, ‘a’].
v = ['a', 'b', 'c', 'd', 'e']
w = v[ : :-2]
print(w)5. Adding Elements#
Let’s learn how to add elements to a list.
It’s a frequently used method, so be sure to learn it well.
List v contains ‘A’ and ‘B’.
Suppose you want to add ‘C’ to the end of the list.
In this case, you can use .append().
By coding v.append('C'), the element ‘C’ is added to the end of list v, making list v become [‘A’, ‘B’, ‘C’].
v = ['A', 'B']
print('before', v)
v.append('C')
print('after', v)If you want to insert an element at a specific index, you can use .insert().
As shown in the following code, by coding v.insert(0, 'C'), the element ‘C’ is inserted at index 0 of list v.
Therefore, list v becomes [‘C’, ‘A’, ‘B’].
v = ['A', 'B']
print('before', v)
v.insert(0, 'C')
print('after', v)When appending one list to the end of another list, you can use .extend() as shown in the following code.
List v is [‘A’, ‘B’] and list w is [‘C’, ‘D’].
By coding v.extend(w), list w is appended to the end of list v.
Therefore, list v becomes [‘A’, ‘B’, ‘C’, ‘D’].
v = ['A', 'B']
print('before', v)
w = ['C', 'D']
v.extend(w)
print('after', v)6. Deleting Elements#
Let’s learn how to delete elements from a list.
In the following code, list v is [‘A’, ‘B’].
To delete the element with the value ‘A’, you can code it as v.remove('A').
The .remove() method deletes the element with the specified value.
v = ['A', 'B']
print('before', v)
v.remove('A')
print('after', v)If you want to delete an element from a list by index, you can use the del statement.
As shown in the following code, del v[0] means deleting the element at index 0 of list v.
v = ['A', 'B']
print('before', v)
del v[0]
print('after', v)