I have written a function in Python which includes a loop and some conditional statements. I would like to know how I can simplify the code.
The program is supposed to do the following:
Write a function called "middle" that takes a list and returns a new list that contains all but the first and last elements.
I am using an "if" statement and three "elif" statements where two of these "elif" statements have two lines of code repeated. The program works perfectly. But, I have a sense that it can be written in a more professional (i.e., elegant and shorter) way.
def middle():
i=0
list=[] #an empty list
while True:
entry=input("Enter the list memeber: ")
if entry !="done":
list.append(entry)
i=i+1
elif i==0:
print("Your list is empty :(!")
exit()
elif i==1:
del list[0]
print("The remaining list is: ", list)
exit()
elif i>=2:
del list[0]
del list[-1]
print("The remaining list is: ", list)
exit()
middle()