-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04DS_List.py
184 lines (115 loc) · 2.07 KB
/
04DS_List.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#Data Structure - List
# -*- coding: utf-8 -*-
#Data Structure - List
#List, Tuple, Set, Dictionary, Frozenset
# Hetrogeneous, Mutable, Ordered, Indexed
L1 = [1,2.6, 'Vikas', 'Khullar', True]
#mixed, ordered
L1
print(L1)
#index starts from 0 ; R starts from 1
L1[3]
#change values # mutable
L1[2] = 'YES'
L1
#ordered
L1[0], L1[1], L1[3], L1[4]
L1[5] #does not exist
#another way to print
L1 = [1,2, 'Vikas', 'Khullar', True]
for i in L1:
print(i)
#see indentation in line above
for i in L1: print(i) #same as obove
#range
range(5); range(len(L1))
for i in range(5) : print(i, end =' ')
#ends - how wrap the values
for i in range(len(L1)) : print(L1[i])
#other functions
L1 = [1,2, 3, 'Vikas', True, 'Vikas', "Vikas"]
L1[3]
L1[3]=L1[3].upper()
L1
L1[0:2] #starts at 0, ends at 1 position
sum(L1[0:4])
#Hold
#L1.count(L1[4])
L1.count('Vikas')
L1.count('Khullar') #how many times Khullar found
len(L1) #no of elements
L1.append('Neha')
L1
L1.remove(2)
L1
L1.pop()
L1.pop() #remove last index values
L1.pop(0) #remove 0th position
L1
del L1[0] #removes index value
L1
L1.clear() #clear all values
L1
# Copy List
L1 = [1,2, 'Vikas', 'Khullar', True]
L1
#method 1
L2 = L1
L2
L1.append('Aman')
L2, L1
#The two variables are referencing to same location
L1.append('Vikas')
L2, L1
#method2
L1 = [1,2, 'Vikas', 'Khullar', True]
L3 = L1.copy()
L1.append('Aman')
L3, L1
L1.append('Aman')
L1
L3 #Aman not here
#method3
L1 = [1,2, 'Vikas', 'Khullar', True]
L4 = list(L1)
L1, L4
L1.append('Aman')
L1, L4
#Methods in list
#append, clear, copy, count, extend, index, insert, pop, remove, reverse, sort
L1= list(range(11))
L1
L6=[]
for i in L1:
L6.append(i*i)
L6
#another list
L5 = [i*i for i in range(11)]
L5
L5=L1
L5
L5.reverse()
L5
L5=[3,5,2,6,1]
L5
L5.sort()
L5
#inplace sorting, permanent changes
#Ex
ruits.sort()
fruits
#put mango in 2nd position
fruits.insert(1, 'mango')
fruits
fruits.sort()
fruits
first=50
end=100
range(100)
range(50, 100)
#Join two lists
L1
L5
L6 = L1 + L5
L6
#https://learnbatta.com/blog/python-working-with-lists-51/