-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.py
72 lines (59 loc) · 1.58 KB
/
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
# Iterate over a list using a for loop
list = [1, 2, 3, 4, 5]
print(list)
print("iterating using for loop : ")
for i in range(len(list)):
print(list[i],end=' ')
print('\n')
# Iterate over a list using a while loop
i = 0
print("The values of the list are :")
while i < len(list):
print(list[i],end=' ')
i += 1
print('\n')
# Iterate over a list using list comprehension
print("Iterating using comprehension :",[i for i in list])
print('\n')
# Iterate over a list using enumerate()
print("Iterating using enumerate: ")
for i, item in enumerate(list):
print(i, item)
print('\n')
# Append an element to a list
list.append(6)
print("Adding an element to list: ")
print(list)
print('\n')
# Remove an element from a list by index
list.pop(0)
print("List after removing first element from a list by index :",list)
print('\n')
# Remove an element from a list by value
list.remove(3)
print("List after removing a value(3) is :",list)
print('\n')
# Sort a list
list.sort()
print("Sorted list is :",list)
print('\n')
# Reverse a list
list.reverse()
print("Reversed list is :",list)
print('\n')
# Count the number of occurrences of an element in a list
count = list.count(2)
print('The number of occurance of 2 is :',count)
print('\n')
# Find the index of an element in a list
index = list.index(4)
print("Index of element 4 is :",index)
print('\n')
# Get the minimum element in a list
min_element = min(list)
print("Minimum element is :",min_element)
print('\n')
# Get the maximum element in a list
max_element = max(list)
print("Maximum element is :",max_element)
print('\n')