I want to delete a sublist from a list. I only know the starting index and the ending index. how can I delete from the list?
I'm using the following code:
def delete_sub_list( self, cmd_list, start, end ):
tmp_lst = []
for i in range( len( cmd_list ) ):
if( i < start or i > end ):
tmp_lst.append( cmd_list[ i ] )
return tmp_lst
and I'm calling in the following way:
cmd_list = self.delete_sub_list( cmd_list, 4, 14 )
The Python syntax to do this is
del cmd_list[4:14 + 1]
(The + 1
is necessary to match your code. Python uses half-open intervals, i.e. the first index is included in the slice, but the last isn't.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With