In: Computer Science
write a program in python that insert a number in the middle of an array. Assume that the length of an array is even. For instance, is a=(1,4,7,9) and num=100, then really=(1,4,100,7,9)
Hi, here is your code. Please give an upvote. If you have any doubts regarding my answer, please tell me in the comments. I would be very happy to help you again.
CODE TO COPY:
# Declare the list
a = []
# Entering list size from user(Enter even size)
size = int(input("Enter size of list: "))
# User will enter the elements in the list
print("Enter the elements in the list: ")
# This for loop is for taking user input
# and then storing it in the list
for i in range(size):
element = int(input())
# appending the element in the list
a.append(element)
# Printing the list
print(a)
# Asking the user to enter the new element
newElement = int(input("Enter the new element:"))
# Inserting the the new element in the list
a.insert( size//2, newElement)
# Printing the new list
print(a)
CODE SCREENSHOT:
CODE
OUTPUT: