Python program to add one or more element in List
top of page

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]



47 views0 comments

Recent Posts

See All

Python program to calculate the factorial of a number

Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. This program is most for O Level Practical Exam so read it care

NumPy program to find the most frequent value in the list.

This program is most for O Level Practical Exam so read it carefully. Program: import numpy as np x = np.array([1,2,3,4,5,7,2,1,1,1,8,9,1]) print(x) print("most frequent value in the list:") print(np.

bottom of page