Skip to content

Commit 6123219

Browse files
author
majid shahbaz
committed
added List Readme.md
1 parent 430f01f commit 6123219

File tree

2 files changed

+89
-102
lines changed

2 files changed

+89
-102
lines changed

src/datastructures/lists/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
## This is the documentation about list in python.
2+
3+
### * What is a List?
4+
Lists are used to store multiple items in a single variable.
5+
Lists are one of 4 built-in data types in Python used to store collections of data,
6+
the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
7+
8+
### 1. List Detail
9+
Lists are created using square brackets:
10+
list = []
11+
12+
Properties of List:
13+
1. List items are ordered
14+
2. List is a mutable collection
15+
3. Lists can have duplicate items
16+
4. List items can be accessed using the index e.g. list[0]
17+
18+
### 1.1. Ordered
19+
When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
20+
21+
If you add new items to a list, the new items will be placed at the end of the list.
22+
E.g.
23+
```python
24+
numbers = [10, 20, 30]
25+
numbers.append(40)
26+
print(numbers) # Output: [10, 20, 30, 40]
27+
```
28+
### 1.2. Mutable (Changeable)
29+
1.2.1. We can add new items in list
30+
1.2.2. We can remove items from list
31+
1.2.3. We can change items in list
32+
1.2.4. We can delete items from list
33+
34+
**1.2.1. Adding Elements**
35+
```python
36+
a = [1, 2, 3]
37+
a.extend([4, 5]) # [1, 2, 3, 4, 5]
38+
a.insert(0, 0) # [0, 1, 2, 3, 4, 5]
39+
```
40+
41+
**1.2.2. Removing Elements**
42+
```python
43+
a = [1, 2, 3]
44+
a.pop() # removes last element
45+
a.pop(0) # removes first element
46+
a.remove(3) # removes first occurrence of 3
47+
del a[1] # deletes element at index 1
48+
```
49+
50+
**1.2.3. Updating Elements**
51+
```python
52+
a = [1, 2, 3]
53+
a[0] = 100 # change first element to 100
54+
print(a) # [100,2,3]
55+
```
56+
57+
**1.2.4. Deleting Slices**
58+
```python
59+
a = [1, 2, 3]
60+
del a[1:3] # delete a range
61+
del a[:] # delete entire list content
62+
```
63+
64+
---
65+
66+
### 2.1. List Methods
67+
68+
| Method | Description |
69+
|---------------------|-------------|
70+
| `append(x)` | Add item `x` to the end |
71+
| `extend(iterable)` | Add all elements from iterable |
72+
| `insert(i, x)` | Insert `x` at index `i` |
73+
| `remove(x)` | Remove first item with value `x` |
74+
| `pop([i])` | Remove and return item at index `i` |
75+
| `clear()` | Remove all items |
76+
| `index(x)` | Return index of first item with value `x` |
77+
| `count(x)` | Count occurrences of `x` |
78+
| `sort()` | Sort the list |
79+
| `reverse()` | Reverse the list |
80+
| `copy()` | Return a **shallow copy** |
81+
82+
---
83+
84+
📝 **Note on Copying**
85+
- `list.copy()` and `list[:]` both create a **shallow copy**.
86+
- Shallow copy means the outer list is copied, but nested objects still refer to the original.
87+
88+
---

src/datastructures/lists/list.py

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,4 @@
11
# LISTS
22
"""
3-
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
1054
"""

0 commit comments

Comments
 (0)