Repository navigation
Expand file tree
/
Copy path2.2.e.Python_Lists_best Examples_Set 2.py
More file actions
121 lines (85 loc) · 3.47 KB
/
Copy path2.2.e.Python_Lists_best Examples_Set 2.py
File metadata and controls
121 lines (85 loc) · 3.47 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()…)
#Some of the list methods are mentioned in set 1 below
#List Methods in Python | Set 1 (in, not in, len(), min(), max()…)
#More methods are discussed in this article.
#1. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.
#2. pop() :- This method deletes the element at the position mentioned in its arguments.
# Python code to demonstrate the working of
# del and pop()
'''# initializing list
lis = [2, 1, 3, 5, 4, 3, 8]
# using del to delete elements from pos. 2 to 5
# deletes 3,5,4
del lis[2 : 5]
# displaying list after deleting
print ("List elements after deleting are : ",end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")
print("\r")
# using pop() to delete element at pos 2
# deletes 3
lis.pop(2)
# displaying list after popping
print ("List elements after popping are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")'''
#3. insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.
#4. remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.
# Python code to demonstrate the working of
# insert() and remove()
'''# initializing list
lis = [2, 1, 3, 5, 3, 8]
# using insert() to insert 4 at 3rd pos
lis.insert(3, 4)
# displaying list after inserting
print("List elements after inserting 4 are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")
print("\r")
# using remove() to remove first occurrence of 3
# removes 3 at pos 2
lis.remove(3)
# displaying list after removing
print ("List elements after removing are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")'''
#5. sort() :- This function sorts the list in increasing order.
#6. reverse() :- This function reverses the elements of list.
# Python code to demonstrate the working of
# sort() and reverse()
'''# initializing list
lis = [2, 1, 3, 5, 3, 8]
# using sort() to sort the list
lis.sort()
# displaying list after sorting
print ("List elements after sorting are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")
print("\r")
# using reverse() to reverse the list
lis.reverse()
# displaying list after reversing
print ("List elements after reversing are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")'''
#7. extend(b) :- This function is used to extend the list with the elements present in another list. This function takes another list as its argument.
#8. clear() :- This function is used to erase all the elements of list. After this operation, list becomes empty.
# Python code to demonstrate the working of
# extend() and clear()
'''# initializing list 1
lis1 = [2, 1, 3, 5]
# initializing list 1
lis2 = [6, 4, 3]
# using extend() to add elements of lis2 in lis1
lis1.extend(lis2)
# displaying list after sorting
print ("List elements after extending are : ", end="")
for i in range(0, len(lis1)):
print(lis1[i], end=" ")
print ("\r")
# using clear() to delete all lis1 contents
lis1.clear()
# displaying list after clearing
print ("List elements after clearing are : ", end="")
for i in range(0, len(lis1)):
print(lis1[i], end=" ")'''