-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListMethods.py
86 lines (64 loc) · 1.83 KB
/
ListMethods.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
#def add(a,b):
# res=a+b
# return res
'''
Python List Methods
append() - Add an element to the end of the list
extend() - Add all elements of a list to the another list
insert() - Insert an item at the defined index
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items passed as an argument
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
copy() - Returns a shallow copy of the list
'''
# animal list
animal = ['cat', 'dog', 'rabbit']
# another list of wild animals
wild_animal = ['tiger', 'fox']
# adding wild_animal list to animal list
animal.append(wild_animal)
#Updated List
print('Updated animal list: ', animal)
'''# animal list
animal = ['cat', 'dog', 'rabbit']
# an element is added
animal.append('guinea pig')
#Updated Animal List
print('Updated animal list: ', animal)
'''
# vowel list
vowel = ['a', 'e', 'i', 'u']
# inserting element to list at 4th position
vowel.insert(3, 'o')
print('Updated List: ', vowel)
# animal list
animal = ['cat', 'dog', 'rabbit', 'guinea pig']
# 'rabbit' element is removed
animal.remove('rabbit')
#Updated Animal List
print('Updated animal list: ', animal)
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# count element 'i'
count = vowels.count('i')
# print count
print('The count of i is:', count)
# count element 'p'
count = vowels.count('p')
# print count
print('The count of p is:', count)
# vowels list
vowels = ['e', 'a', 'u', 'o', 'i']
# sort the vowels
vowels.sort()
# print vowels
print('Sorted list:', vowels)
old_list = [1, 2, 3]
new_list = old_list
# add element to list
new_list.append('a')
print('New List:', new_list )
print('Old List:', old_list )