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
Lists are used to store multiple items in a single variable.
4
-
Lists are one of 4 built-in data types in Python used to store collections of data,
5
-
the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
6
-
7
-
Lists are created using square brackets:
8
-
list = []
9
-
10
-
Properties of List:
11
-
1. List items are ordered
12
-
2. List is a mutable collection
13
-
3. Lists can have duplicate items
14
-
4. List items can be accessed using the index e.g. list[0]
15
-
16
-
1.1. ORDERED
17
-
When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
18
-
19
-
If you add new items to a list, the new items will be placed at the end of the list.
20
-
E.g.
21
-
list_a = [1,2,3]
22
-
list_a.append(4)
23
-
print(list_a) -> [1,2,3,4]
24
-
25
-
1.2. Changeable (Mutable)
26
-
1.2.1. We can add new items in list
27
-
1.2.2. We can remove items from list
28
-
1.2.3. We can change items in list
29
-
1.2.4. We can delete items from list
30
-
31
-
E.g. list_b = [1,2,3,4]
32
-
=> Add new items to list
33
-
list_b.append(5)
34
-
print(list_b) -> [1,2,3,4,5]
35
-
36
-
=> Remove items from list
37
-
note: list.pop() removes the last item in the list
38
-
list_b.pop()
39
-
print(list_b) -> [1,2,3,4]
40
-
We can provide the position of element to remove
41
-
list_b.pop([0]) -> [2,3,4]
42
-
43
-
=> Change the items in the list
44
-
list_c = [1,2,3,4]
45
-
list_c[0] = 6
46
-
print(list_c) -> [6,2,3,4]
47
-
48
-
=> Delete items from list
49
-
Two ways to delete a item
50
-
1. POP
51
-
list.pop([i])
52
-
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
53
-
It raises an IndexError if the list is empty or the index is outside the list range.
54
-
2. Remove
55
-
list.remove(x)
56
-
Remove the first item from the list whose value is equal to x.
57
-
It raises a ValueError if there is no such item.
58
-
59
-
And another way you can delete a item by index
60
-
del(list_c[0]) -> it don't return a new list after delete but pop return a list of items.
61
-
The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice)
62
-
Delete a slice of items
63
-
del a[2:4]
64
-
Delete a entire list
65
-
del a[:]
66
-
"""
67
-
"""
68
-
5.1. More on Lists
69
-
The list data type has some more methods. Here are all of the methods of list objects:
70
-
71
-
1. list.append(x)
72
-
Add an item to the end of the list. Similar to a[len(a):] = [x].
73
-
74
-
2. list.extend(iterable)
75
-
Extend the list by appending all the items from the iterable. Similar to a[len(a):] = iterable.
76
-
77
-
3. list.insert(i, x)
78
-
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
79
-
80
-
4. list.remove(x)
81
-
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
82
-
83
-
5. list.pop([i])
84
-
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. It raises an IndexError if the list is empty or the index is outside the list range.
85
-
86
-
6. list.clear()
87
-
Remove all items from the list. Similar to del a[:].
88
-
89
-
7. list.index(x[, start[, end]])
90
-
Return zero-based index in the list of the first item whose value is equal to x.
91
-
Raises a ValueError if there is no such item.
92
-
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
93
-
94
-
8. list.count(x)
95
-
Return the number of times x appears in the list.
96
-
97
-
9. list.sort(*, key=None, reverse=False)
98
-
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
99
-
100
-
10. list.reverse()
101
-
Reverse the elements of the list in place.
102
-
103
-
11. list.copy()
104
-
Return a shallow copy of the list. Similar to a[:].
3
+
To check the detail about list please refer to README.md in the same list folder
0 commit comments