-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.py
More file actions
72 lines (57 loc) · 1.22 KB
/
Copy pathlist.py
File metadata and controls
72 lines (57 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
list1 = [1, 2, 3]
print(list1)
list1.append(4)
print(list1)
# inserting in specific position
list1.insert(2, 7)
print(list1)
# .pop() removes the last item and also returns it
last_item = list1.pop()
# print(last_item)
ls = list()
ls.append(last_item)
print(ls)
list1.append(7)
print(list1)
# .remove(x) only removes one element of the list which is the first one
list1.remove(7)
print(list1)
ls.clear()
print(ls)
list1.reverse()
print(list1)
list1.sort()
print(list1)
# accesing elements from behind
print(list1[-1])
print(list1[-2])
print(list1[-3])
print(list1[-4])
# print(list1[-5]) out of bounds
# sorting without changing element in places
new_list = sorted(list1)
print(new_list)
l3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l3[1:5])
print(l3[::2])
print(l3[::-1])
print(l3[::-1] + l3[1:])
# copying list
l4 = [1, 2, 3]
l5 = l4
l5.append(4)
# l4 also appends new elements because the starting pointer of l4 becomes the same pointer after assigning it to l5
print(l4)
# makes actual copy with different pointers
l5 = l4.copy()
l5 = list(l4)
l5 = l4[:]
# list comprehension
l6 = [1, 2, 3, 4]
l7 = [i * i for i in l6]
print(l7)
l8 = [0] * 5
print(l8)
print(l8.count(0))
l7 = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11]
print("List", l7[0:9:2])