-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.Methods for working with Lists
55 lines (40 loc) · 1.35 KB
/
27.Methods for working with Lists
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
#Lists are mutable where elements can be modified after they has been formed.
l = [1, "Sri", 9.087, 'c', "Ram"]
print(type(l))
#Output: <class 'list'>
l.append(1) #Adding an element at end of the list
print(l)
#Output: [1, 'Sri', 9.087, 'c', 'Ram', 1]
l.insert(3, 10.768) #insert(position, item)
print(l)
#Output: [1, 'Sri', 9.087, 10.768, 'c', 'Ram', 1]
print(l.count("Ram")) #Count the number of occurence of an item
#Output: 1
print(l.index(9.087)) #Gives the index position of an item
#Output: 2
print(l.pop(1)) #pop(index)
#Output: Sri
print(l.pop()) #removes last item
#Output: 1
l.remove('c') #removes the letter 'c' in the list
print(l)
#Output: [1, 9.087, 10.768, 'Ram']
l.reverse() #Reversing a list
print(l)
#Output: ['Ram', 10.768, 9.087, 1]
s = l.copy() #Make a copy of the list
print(s)
#Output: ['Ram', 10.768, 9.087, 1]
del s[2] #To delete single value
print(s)
#Output: ['Ram', 10.768, 1]
del s #To delete whole list
l.clear() #Removes all the elements from the list
print(l)
#Output: []
'''
Note:
The pop() method deletes the final element or the element specified by the index.
The remove() method deletes the first instance of the provided element.
The del keyword delete any variable, list of values from a list.
'''