How to delete the middle element of a list in Python

2 Answers

0 votes
def deleteMiddleElement(st,  size,  current) :
    if ((len(st) == 0) or current == size) :
        return
    el = st[-1]
    
    st.pop()
    
    deleteMiddleElement(st, size, current + 1)
    
    if (current != int(size / 2)) :
        st.append(el)
    
    
st =  []
st.append('3')
st.append('5')
st.append('1')
st.append('m')
st.append('9')
st.append('2')
st.append('7')
        
deleteMiddleElement(st, len(st), 0)

print(st)




'''
run:

['3', '5', '1', '9', '2', '7']

'''

 



answered May 27, 2023 by avibootz
0 votes
st =  []
st.append('3')
st.append('5')
st.append('1')
st.append('m')
st.append('9')
st.append('2')
st.append('7')
        
del st[int(len(st) / 2)]

print(st)



'''
run:

['3', '5', '1', '9', '2', '7']

'''

 



answered May 27, 2023 by avibootz
...