Python program to add one or more element in List


Program:

#Append() is used for adding one element in List at the end of list.

X=[9,18,27]

X.append(36)

print(X)

output: [9, 18, 27, 36]

#Insert () is used for add element at any given position in the list.

X.insert(2,20)

print(X)

Output: [9, 18, 20, 27, 36]

#It represent add element add the new element 10 at 2nd position in the list from the back.

X.insert(-2,10)

print(X)

Output: [9, 18, 20, 10, 27, 36]

#Extend() is used to add one list into another. The list to be added gets added at the end of the first list.

Y=[45,54]

X.extend(Y)

print(X)

Output; [9, 18, 20, 10, 27, 36, 45, 54]



17 views0 comments

Recent Posts

See All