Repository navigation
Expand file tree
/
Copy path2.2.c.Python_Lists_best Examples.py
More file actions
129 lines (87 loc) · 3.71 KB
/
Copy path2.2.c.Python_Lists_best Examples.py
File metadata and controls
129 lines (87 loc) · 3.71 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
122
123
124
125
126
127
128
129
#List methods in Python
#Adding and Appending
#1)append(): Used for appending and adding elements to List.It is used to add elements to the last position of List.
#Syntax: list.append (element)
# Adds List Element as value of List.
'''List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)'''
#2)insert(): Inserts an elements at specified position.
#Syntax: list.insert(<position, element)
#Note: Position mentioned should be within the range of List, as in this case between 0 and 4, elsewise would throw IndexError.
'''List = ['Mathematics', 'chemistry', 1997, 2000]
# Insert at index 2 value 10087
List.insert(2,10087)
print(List)'''
#3)extend(): Adds contents to List2 to the end of List1.
#Syntax: List1.extend(List2)
'''List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
# Add List2 to List1
List1.extend(List2)
print(List1)
#Add List1 to List2 now
List2.extend(List1)
print(List2)'''
#sum(), count(), index(), min() and max() functions of List
#4)sum() : Calculates sum of all the elements of List.
#Syntax: sum(List)
'''List = [1, 2, 3, 4, 5]
print(sum(List))'''
#What happens if numeric value is not used a parameter?
#Sum is calculated only for Numeric values, elsewise throws TypeError.
#See example:
'''List = ['gfg', 'abc', 3]
print(sum(List))'''
#5)count():Calculates total occurrence of given element of List.
#Syntax: List.count(element)
'''List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))'''
#6)index(): Returns the index of first occurrence. Start and End index are not necessary parameters.
#Syntax: List.index(element[,start[,end]])
'''List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))'''
#Another example:
'''List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2))
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
# will check from index 2 to 3.
print(List.index(2,2,4)) '''
#7) min() : Calculates minimum of all the elements of List.
#Syntax: min(List)
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(min(List))'''
#8)max(): Calculates maximum of all the elements of List.
#Syntax: max(List)
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(max(List))'''
#sort() and reverse() functions
#9)reverse(): Sort the given data structure (both tuple and list) in ascending order. Key and reverse_flag are not necessary parameter and reverse_flag is set to False, if nothing is passed through sorted().
#Syntax: sorted([list[,key[,Reverse_Flag]]]) list.sort([key,[Reverse_flag]])
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
#Reverse flag is set True
List.sort(reverse=True)
#List.sort().reverse(), reverses the sorted list
print(List)
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
sorted(List)
print(List)'''
#Deletion of List Elements
#To Delete one or more elements, i.e. remove an element, many built in functions can be used, such as pop() & remove() and keywords such as del.
#10)pop(): Index is not a necessary parameter, if not mentioned takes the last index.
#Syntax: list.pop([index])
#Note: Index must be in range of the List, elsewise IndexErrors occurs.
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())'''
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop(0))'''
#del() : Element to be deleted is mentioned using list name and index.
#Syntax: del list.[index]
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
del List[0]
print(List)'''
#remove(): Element to be deleted is mentioned using list name and element.
#Syntax: list.remove(element)
'''List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)'''