Remove last entry in vector/list?

0 Sorin-Mihai Oprea · October 3, 2015
Why am I getting this when I'm trying to remove the last entry?
>>> players=[120, 24, 56]
>>> players
[120, 24, 56]
>>> players.append(1)
>>> players
[120, 24, 56, 1]
>>> players[-1]=[]
>>> players
[120, 24, 56, []]
>>> 


Post a Reply

Replies

Oldest  Newest  Rating
0 Halcyon Abraham Ramirez · October 3, 2015
use pop or del

your_list = [1,2,3,4,5]
your_list.pop()
print(your_list)
>>>>>[1,2,3,4]
0 Sorin-Mihai Oprea · October 4, 2015
Thanks Superman. Didn't know about the del command.
0 Sorin-Mihai Oprea · October 4, 2015
@Halcyon: Is there also a push? Like pop and push?
0 sfolje 0 · October 4, 2015
3th solution (2th alternative):

#Use no command.
>>> players=[120, 24, 56, 1]
>>> players
[120, 24, 56, 1]
>>> players=players[:-1]
>>> players
[120, 24, 56]
>>>
0 Halcyon Abraham Ramirez · October 4, 2015
there is no push in python

the push equivalent of python is 

your_list.append("some item")
  • 1

Python

107,301 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator