-2

I am trying to remove a position from an empty list, as a simple example that demonstrates or is trying to remove.

#Test about fruits

fruits = []

fruits.append('maça')
fruits.append('goiaba')
fruits.append('uva')
fruits.append('pera')
fruits.append('bananna')
fruits[1] = []

print (fruits)

Output : ['maça', [], 'uva', 'pera', 'bananna']

Desired output ['maça', 'uva', 'pera', 'bananna']

Just remembering that it is a simple example, so I can apply it in a more robust program that I am developing that presents the same problem of having a "position" in the list with a 'null' or empty value in this case.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Janio Carvalho
  • 117
  • 1
  • 2
  • 10

2 Answers2

0

Use pop() to remove an element from a list by its index, rather than assigning an empty value to the index.

fruits.pop(1)

or del

del fruits[1]

pop() allows you to retrieve the value being removed at the same time.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can use

del fruits[1]

See Difference between del, remove and pop on lists for the differences.

user0
  • 185
  • 11
  • Sorry, I hadn't seen the other answer had been edited. The link to the other question is still useful. – user0 Jan 27 '20 at 20:02