2023-02-14 09:16:11 +00:00
|
|
|
---
|
|
|
|
tags: [python, data-structures]
|
|
|
|
---
|
|
|
|
|
|
|
|
# Lists in Python
|
|
|
|
|
|
|
|
Lists are the equivalent of a simple array in JavaScript.
|
|
|
|
|
|
|
|
Lists have the following properties:
|
|
|
|
|
|
|
|
- They are **ordered**
|
|
|
|
- They are **mutable** and can be modified
|
|
|
|
- They **allow duplicate** members
|
|
|
|
- They are **indexed**
|
|
|
|
- You can increase/decrease their length by adding/removing new members
|
2023-02-15 07:36:48 +00:00
|
|
|
|
|
|
|
> Lists are denoted with `[...]`
|
|
|
|
|
2023-02-16 15:15:16 +00:00
|
|
|
## Duplicating lists
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
When we want to duplicate a list, we can't just reassign the list to a new
|
|
|
|
variable and expect this to be a copy.
|
2023-02-16 15:15:16 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
If we edit this "copy" it will update the original list since it copies the
|
|
|
|
pointer and will therefore point to the same address in memory. Instead we have
|
|
|
|
to use the List `copy()` function which returns a new list and doesn't modify
|
|
|
|
the original list.
|
2023-02-16 15:15:16 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Relatedly, we should distinguish those List methods and functions which create a
|
|
|
|
new list (non-destructive) and those that mutate the existing list.
|
2023-02-16 15:15:16 +00:00
|
|
|
|
2023-02-15 07:36:48 +00:00
|
|
|
## Basic usage
|
|
|
|
|
|
|
|
```python
|
|
|
|
# Defining a list
|
|
|
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
|
|
|
list2 = [4]
|
|
|
|
|
|
|
|
# Empty list
|
|
|
|
list3 = [] # empty list
|
|
|
|
list3 = list() # Also empty list
|
|
|
|
|
|
|
|
# Nested list
|
|
|
|
list5 = [[2, 3], [6, 8]]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Slicing
|
|
|
|
|
|
|
|
```python
|
|
|
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
|
|
|
|
|
|
|
print(list1[1])
|
|
|
|
print(list1[-1])
|
|
|
|
print(list1[1:3])
|
|
|
|
print(list1[:3])
|
|
|
|
print(list1[1:])
|
|
|
|
|
|
|
|
"""
|
2023-08-31 20:40:51 +01:00
|
|
|
'John'
|
|
|
|
'Ringo'
|
2023-02-15 07:36:48 +00:00
|
|
|
['Paul', 'George']
|
|
|
|
['John', 'Paul', 'George']
|
|
|
|
['Paul', 'George', 'Ringo']
|
|
|
|
"""
|
|
|
|
```
|
|
|
|
|
|
|
|
## Adding additional values to existing list
|
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
### Single value
|
|
|
|
|
|
|
|
```py
|
2023-02-15 07:36:48 +00:00
|
|
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
|
|
|
|
|
|
|
# Add single element to the end of a list
|
|
|
|
list1.append('Pete')
|
|
|
|
# ['John', 'Paul', 'George', 'Ringo', 'Pete']
|
2023-08-31 20:40:51 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Multiple values
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
```py
|
2023-02-15 07:36:48 +00:00
|
|
|
list1.extend(['Albert', 'Bob'])
|
|
|
|
list1 += ['Ginger', 'Sporty']
|
|
|
|
# ['John', 'Paul', 'George', 'Ringo', 'Pete', 'Albert', 'Bob', 'Ginger', 'Sporty']
|
2023-08-31 20:40:51 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Specific index
|
|
|
|
|
|
|
|
```python
|
2023-02-15 07:36:48 +00:00
|
|
|
|
|
|
|
## Insert at specific index
|
|
|
|
|
|
|
|
a_list = ['Adele', 'Madonna', 'Cher']
|
|
|
|
a_list.insert(1, 'Paloma')
|
|
|
|
print(a_list)
|
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
# ['Adele', 'Paloma', 'Madonna', 'Cher']
|
2023-02-15 07:36:48 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Removing elements
|
|
|
|
|
|
|
|
We distinguish `del` from `remove` when removing elements from lists:
|
|
|
|
|
|
|
|
- `del` requires an index value
|
2024-02-02 15:58:13 +00:00
|
|
|
- `remove` requires a value reference (i.e. the name of the element rather than
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
`del` is simple deletion whereas `remove` searches the list. Therefore `del` is
|
|
|
|
more efficient.
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
### `del`
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
```py
|
|
|
|
my_list = [10, 20, 30, 40, 50]
|
|
|
|
del my_list[1]
|
|
|
|
print(my_list) # Output will be [10, 30, 40, 50]
|
|
|
|
```
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
We can remove multiple items at once via a slice:
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
```py
|
|
|
|
my_list = [10, 20, 30, 40, 50]
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
# Delete the elements from index 1 to 3 (inclusive of start index and exclusive of end index)
|
|
|
|
del my_list[1:4]
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
# Print the updated list
|
|
|
|
print(my_list) # Output will be [10, 50]
|
|
|
|
```
|
2023-02-15 07:36:48 +00:00
|
|
|
|
2023-08-31 20:40:51 +01:00
|
|
|
### `remove()`
|
|
|
|
|
|
|
|
```py
|
|
|
|
my_list = [10, 20, 30, 40, 50]
|
|
|
|
|
|
|
|
# Remove the element with value 30
|
|
|
|
my_list.remove(30)
|
|
|
|
|
|
|
|
# Print the updated list
|
|
|
|
print(my_list) # Output will be [10, 20, 40, 50]
|
2023-02-15 07:36:48 +00:00
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> If the value is not found in the list,
|
2024-11-01 16:51:35 +00:00
|
|
|
> `remove()`` will raise a ValueError. To avoid this, you can check whether the value exists in the list before calling `remove()`
|
2023-08-31 20:40:51 +01:00
|
|
|
|
|
|
|
### pop()
|
|
|
|
|
|
|
|
Remove and return the element removed
|
|
|
|
|
|
|
|
```python
|
|
|
|
list6 = ['Once', 'Upon', 'a', 'Time']
|
|
|
|
print(list6.pop(2))
|
|
|
|
# a
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Return index of a list element
|
2023-02-15 07:36:48 +00:00
|
|
|
|
|
|
|
```python
|
|
|
|
list7 = [2, 3, 6, 8]
|
|
|
|
print(list7.index(8))
|
|
|
|
# 3
|
|
|
|
|
|
|
|
list6 = ['Once', 'Upon', 'a', 'Time']
|
|
|
|
print(list6.index('a'))
|
|
|
|
# 2
|
|
|
|
```
|
|
|
|
|
|
|
|
## Nesting lists
|
|
|
|
|
|
|
|
```python
|
|
|
|
l1 = [1, 43.5, 'Phoebe', True]
|
|
|
|
l2 = ['apple', 'orange', 31]
|
|
|
|
root_list = ['John', l1, l2, 'Denise']
|
|
|
|
print(root_list)
|
|
|
|
# ['John', [1, 43.5, 'Phoebe', True], ['apple', 'orange', 31], 'Denise']
|
|
|
|
```
|
|
|
|
|
2024-11-01 16:51:35 +00:00
|
|
|
## Merging lists
|
2023-04-08 11:42:25 +01:00
|
|
|
|
|
|
|
```py
|
|
|
|
list1 = [1, 2, 3]
|
|
|
|
list2 = [4, 5, 6]
|
|
|
|
|
|
|
|
merged_list = list1 + list2
|
|
|
|
print(merged_list) # Output: [1, 2, 3, 4, 5, 6]
|
|
|
|
```
|
2023-09-01 09:19:46 +01:00
|
|
|
|
2024-11-01 16:51:35 +00:00
|
|
|
## Flattening a two-dimensional list (matrix)
|
|
|
|
|
|
|
|
Use [list comprehension](List_comprehension_in_Python.md) for each row in the
|
|
|
|
matrix:
|
|
|
|
|
|
|
|
```python
|
|
|
|
matrix = [
|
|
|
|
[9, 3, 8, 3],
|
|
|
|
[4, 5, 2, 8],
|
|
|
|
[6, 4, 3, 1],
|
|
|
|
[1, 0, 4, 5],
|
|
|
|
]
|
|
|
|
|
|
|
|
flat = [item for row in matrix for item in row]
|
|
|
|
# [9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]
|
|
|
|
```
|
|
|
|
|
2023-09-01 09:19:46 +01:00
|
|
|
## See also
|
|
|
|
|
2024-02-17 11:57:44 +00:00
|
|
|
[Sorting lists in Python](Sorting_lists_in_Python.md)
|