You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit caf74a7
Browse filesBrowse the repository at this point in the historyBrowse files
## A list stores many values in a single structure.
21
-
The most popular kind of data collection in Python is the list. Lists have two primary important characteristics:
21
+
A list is one of the most commonly used data structures in Python. Lists have two important characteristics:
22
22
23
-
1. They are mutable, i.e., they can be changed after they are created.
24
-
2. They are heterogeneous, i.e., they can store values of many different types.
23
+
1. They are `mutable`, i.e, they can be changed after they are created.
24
+
2. They are `heterogeneous`, i.e, they can store values of many different types.
25
25
26
26
To create a new list, you can just put some values in square brackets with commas in between. Let's create a short list of some library metadata standards.
27
27
@@ -76,7 +76,7 @@ List is now: ['bibframe', 'frbr', 'mets', 'mods']
76
76
77
77
### Character strings are immutable.
78
78
79
-
Unlike lists, we cannot change the characters in a string using its index value. In other words strings are *immutable* (cannot be changed in-place after creation), while lists are *mutable*: they can be modified in place. Python considers the string to be a single value with parts, not a collection of values.
79
+
Unlike lists, we cannot change the characters in a string using its index value. Strings and lists can both be indexed and sliced. However, strings are immutable (cannot change in place), while lists are mutable.
80
80
81
81
```python
82
82
librarian ='Langanathan'# misspelled SR Ranganathan's name
@@ -100,9 +100,16 @@ f'First item in sublist: {mixed_list[2][0]}'
100
100
First item in sublist: 10
101
101
```
102
102
103
-
## Appending items to a list lengthens it.
103
+
## Using list methods to make adding/removing easier.
104
104
105
-
Use `list_name.append` to add items to the end of a list. In Python, we would call `.append()` a *method* of the list object. You can use the syntax of `object.method()` to call methods.
105
+
Lists are **objects**, which means they provide methods that allow us to perform common operations. A **method** is called using the syntax `object.method()`.
106
+
107
+
Lists provide methods for modifying their contents. We call these methods using dot notation. There are many methods associated with a list, but we will cover only a few of them.
108
+
109
+
110
+
### Appending items to a list.
111
+
112
+
Use `list_name.append()` to add items to the end of a list. In Python, we would call `.append()` a *method* of the list object.
106
113
107
114
```python
108
115
print(f'list was:{metadata}')
@@ -115,22 +122,55 @@ list was: ['bibframe', 'frbr', 'mets', 'mods']
115
122
list is now: ['bibframe', 'frbr', 'mets', 'mods', 'oai-pmh']
116
123
```
117
124
118
-
## Use `del` to remove items from a list entirely.
125
+
We can also call `list_name.insert(index, value)` to add an item at a given position in the list. The first argument specifies where to place the new item, and the second argument is the item to add.
126
+
127
+
```python
128
+
numbers = [1, 2, 3, 4, 6]
129
+
print(f'numbers before: {numbers}')
130
+
numbers.insert(4, 5)
131
+
print(f'numbers after: {numbers}')
132
+
```
133
+
134
+
```output
135
+
numbers before: [1, 2, 3, 4, 6]
136
+
numbers after: [1, 2, 3, 4, 5, 6]
137
+
```
138
+
139
+
### Removing items from a list.
140
+
141
+
Use `list_name.pop()` to remove and return the last item in a list. If we provide an index, `.pop()` removes and _returns_ the item at that position. It raises an IndexError if the list is empty or the index is out of range.
119
142
120
-
`del list_name[index]` removes an item from a list and shortens the list. Unlike `.append()`, `del` is not a method, but a "statement" in Python. In the example below, `del` performs an "in-place" operation on a list of prime numbers. This means that the `primes` variable will be reassigned when you use the `del` statement, without needing to use an assignment operator (e.g., `primes = ...`) .
121
143
122
144
```python
123
-
primes = [2, 3, 5, 7, 11]
124
-
print(f'primes before: {primes}')
125
-
del primes[4]
126
-
print(f'primes after: {primes}')
145
+
drinks = ['water', 'tea', 'coffee', 'milk']
146
+
print(f'drinks before: {drinks}')
147
+
drinks.pop()
148
+
print(f'drinks after first pop: {drinks}')
149
+
val = drinks.pop(0)
150
+
print(f'drinks after second pop: {drinks}')
151
+
print(val)
127
152
```
128
153
129
154
```output
130
-
primes before: [2, 3, 5, 7, 11]
131
-
primes after: [2, 3, 5, 7]
155
+
drinks before: ['water', 'tea', 'coffee', 'milk']
156
+
drinks after first pop: ['water', 'tea', 'coffee']
157
+
drinks after second pop: ['tea', 'coffee']
158
+
water
159
+
```
160
+
161
+
We can also call `list_name.remove(value)`, where value is the item you want to remove. This will remove the **first** occurrence of the item. If there are multiple, then `.remove()` will need to be called several times.
0 commit comments