Repository navigation
Expand file tree
/
Copy pathL08_list.py
More file actions
225 lines (186 loc) · 6.9 KB
/
Copy pathL08_list.py
File metadata and controls
225 lines (186 loc) · 6.9 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# list items are ordered, changable, allow duplicates and contains any type of data (even other list).
# in this program we will review how to create, modify and manipulate a list
# list is defined using square brackets "[]"
aList = ["apple", "Orange", "banana", "Cherry"]
print(" ".join(aList))
# we can access the length of the list using len() function
print("Length of this list is", len(aList))
# we can use count() function to count a specified element of a list
print("Counting rhe apples: ", aList.count("apple"))
# we can use index() function to access the first index of a specified value
print("First index of apple is:", aList.index("apple"))
# we can use type() to view type of the variable (it will show its class)
print("Type of the list", type(aList))
# using for loop to print items
print("Print using for loop:")
for item in aList:
print(item, end=" ")
print()
# using index to print items
print("Print list using index:")
for i in range(len(aList)):
print(aList[i], end=" ")
print()
# using negative values as index
print("using negative values as index:")
for i in range(1, len(aList)+1):
print(aList[-i], end=" ")
print()
# using while loop to access the list
print("using while loop:")
i = 0
while (i < len(aList)):
print(aList[i], end=" ")
i += 1
print()
# using list comprehension
print("print using list comprehension:")
[print(i, end=" ") for i in aList]
print()
# overwriting value in a list
aList[:2] = ["blueberry", "watermelon"]
print("overwrite values:")
print(aList)
# insert() function allows us to insert a new element into the list with specified index
aList.insert(2, "berry")
print("using insert() function:")
print(aList)
# append() function allows us to add a single element at the end of the list
aList.append("oranges")
print("using append() function:")
print(aList)
# extend() function allows us to extend the list by appending another list (any other collection) at the end
aList.extend(["apple", "grapes"])
print("using extend() function:")
print(aList)
# using sort() function to sort a list
aList.sort()
print("sorting in ascending order:")
print(aList)
print()
aList.sort(reverse=True)
print("sorting in decending order:")
print(aList)
print()
print("sort with case insensitive:")
aList.sort(key=str.lower)
print(aList)
print()
# using reverse() function to reverse the indexes of the a list
aList.reverse()
print("Reversed list:")
print(aList)
print()
# You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.
# There are ways to make a copy, one way is to use the built-in List method copy().
bList = aList.copy()
# Another way to make a copy is to use the built-in method list().
cList = list(aList)
# using remove() function we can a remove specified value
aList.remove("watermelon")
print("watermelon removed:")
print(aList)
print("second list using copy() function:")
print(bList)
print("third list using list() function:")
print(cList)
print()
# join aList and bList using "+"
abList = aList + bList
print("abList:")
print(abList)
print()
# join aList and cList using extend() function
acList = list()
acList.extend(aList)
acList.extend(cList)
print("acList:")
print(acList)
print()
# join cList and bList using for loop
cbList = cList.copy()
for x in bList:
cbList.append(x)
print("cbList:")
print(cbList)
print()
# to remove a element using a index, we can use pop() function
# if you did not mention the index, by default it will remove the last element.
aList.pop(1)
print("index 1 popped:")
print(aList)
# we can alse use del keyword to remove a element from a list
# to delete an entire list we only need to specify the list name
del aList[-1]
print("Deleting the last element:")
print(aList)
# clear() function used to clear all the elements in the list
aList.clear()
print("Cleared:")
print(aList)
'''
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
'''
'''
the output of this program will be..
apple Orange banana Cherry
Length of this list is 4
Counting rhe apples: 1
First index of apple is: 0
Type of the list <class 'list'>
Print using for..each loop:
apple Orange banana Cherry
Print list using index:
apple Orange banana Cherry
using negative values as index:
Cherry banana Orange apple
using while loop:
apple Orange banana Cherry
print using list comprehension:
apple Orange banana Cherry
overwrite values:
['blueberry', 'watermelon', 'banana', 'Cherry']
using insert() function:
['blueberry', 'watermelon', 'berry', 'banana', 'Cherry']
using append() function:
['blueberry', 'watermelon', 'berry', 'banana', 'Cherry', 'oranges']
using extend() function:
['blueberry', 'watermelon', 'berry', 'banana', 'Cherry', 'oranges', 'apple', 'grapes']
sorting in ascending order:
['Cherry', 'apple', 'banana', 'berry', 'blueberry', 'grapes', 'oranges', 'watermelon']
sorting in decending order:
['watermelon', 'oranges', 'grapes', 'blueberry', 'berry', 'banana', 'apple', 'Cherry']
sort with case insensitive:
['apple', 'banana', 'berry', 'blueberry', 'Cherry', 'grapes', 'oranges', 'watermelon']
Reversed list:
['watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
watermelon removed:
['oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
second list using copy() function:
['watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
third list using list() function:
['watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
abList:
['oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple', 'watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
acList:
['oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple', 'watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
cbList:
['watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple', 'watermelon', 'oranges', 'grapes', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
index 1 popped:
['oranges', 'Cherry', 'blueberry', 'berry', 'banana', 'apple']
Deleting the last element:
['oranges', 'Cherry', 'blueberry', 'berry', 'banana']
Cleared:
[]
'''