DEV Community

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

Posted on • Edited on

1

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

Explica este código Python

Dificultad: Fácil

bebidas = ["Agua", "Coca-Cola", "Pepsi"]
bebidas[0:2] = ["Limonada", "Fanta"]
print(bebidas)
Enter fullscreen mode Exit fullscreen mode

👉 A. ["Pepsi", "Limonada", "Fanta"]
👉 B. ["Limonada", "Fanta", "Agua", "Coca-Cola", "Pepsi"]
👉 C. ["Limonada", "Fanta", "Pepsi"]
👉 D. IndexError


Respuesta:

👉 C. ["Limonada", "Fanta", "Pepsi"]

En Python podemos tomar un intervalo de un lista para poder sustituirlo por otro.

Lograr esto es muy sencillo, solo tenemos que seleccionar el trozo de lista a sustituir y remplazarla por el nuevo trozo.

En nuestro ejemplo cortamos [0:2], ósea ["Agua", "Coca-Cola"], para poder remplazarlo por ["Limonada", "Fanta"].

Es importante mencionar que el trozo a cortar no necesariamente debe ser del mismo tamaño que el trozo a agregar, por ejemplo:

bebidas = ["Agua", "Coca-Cola", "Pepsi"]
bebidas[0:2] = ["Limonada", "Fanta", "Agua con gas", "Sprite"]
print(bebidas) # ['Limonada', 'Fanta', 'Agua con gas', 'Sprite', 'Pepsi']
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more