Skip to content

Commit d83cb3b

Browse files
authored
Create 12 - LIST AND TUPLE OPERATIONS
1 parent 540cde2 commit d83cb3b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

12 - LIST AND TUPLE OPERATIONS

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
A few of the basic list operations used in Python programming are
2+
3+
1.extend(),
4+
2.insert(),
5+
3.append(),
6+
4.remove(),
7+
5.pop(),
8+
6.min()
9+
7.max(),
10+
8.sort(),
11+
9.sorted()
12+
10.index(),
13+
11.clear(),
14+
12.list()
15+
13.tuple(), etc.
16+
17+
18+
1. append()
19+
The append() method is used to add elements at the end of the list. This method can only add a single element at a time.
20+
21+
EXAMPLE:
22+
list1 = [1,2,3,4,5]
23+
list1.append(6) #adding 6 at the end
24+
list1.append(7) #adding 7 at the end
25+
print(list1)
26+
27+
OUTPUT:
28+
[1,2,3,4,5,6,7]
29+
30+
2. extend()
31+
The extend() method is used to add more than one element at the end of the list.
32+
Although it can add more than one element, unlike append(), it adds them at the end of the list like append()
33+
34+
EXAMPLE:
35+
list1 = [1,2,3,4,5]
36+
list1.extend([8,9,10]) #adding 8,9,10 at the end, append use to add single element , extend is use to add multilple elements at the end
37+
print(list1)
38+
39+
OUTPUT:
40+
[1,2,3,4,5,8,9,10]
41+
42+
3. insert()
43+
The insert() method can add an element at a given position in the list.
44+
Thus, unlike append(), it can add elements at any position, but like append(),
45+
it can add only one element at a time. This method takes two arguments.
46+
The first argument specifies the position, and the second argument specifies the element to be inserted.
47+
48+
EXAMPLE:
49+
list1 = [1,2,3,4,5]
50+
list1.insert(0, -100) adding -100 at the 0th index
51+
print(lsit1)
52+
53+
OUTPUT:
54+
[-100, 1,2,3,4,5]
55+
56+
4. remove()
57+
The remove() method is used to remove an element from the list.
58+
In the case of multiple occurrences of the same element, only the first occurrence is removed.
59+
60+
EXAMPLE:
61+

0 commit comments

Comments
 (0)