DEV Community

Cover image for Ibuprofeno.py💊| #97: Explica este código Python
Cristian Fernando
Cristian Fernando

Posted on • Edited on

1

Ibuprofeno.py💊| #97: Explica este código Python

Explica este código Python

Dificultad: Intermedio

numeros = [42,96,12,20,36]
del numeros[100]
print(numeros)
Enter fullscreen mode Exit fullscreen mode

👉 A. TypeError
👉 B. SyntaxError
👉 C. NameError
👉 D. IndexError


Respuesta:

👉 D. IndexError

Para eliminar un item de una lista podemos usar el operador del seguido de la posición que queremos eliminar.

En este sentido, ¿qué sucede si la posición que queremos eliminar no existe dentro de la lista? Entonces el interprete de Python nos regresa un IndexError.

Para solucionar esto debemos eliminar siempre posiciones de la lista que sabemos que existen:

numeros = [42,96,12,20,36]
del numeros[2]
print(numeros) # [42, 96, 20, 36]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay