DEV Community

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

Posted on

1

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

Explica este código Python

Dificultad: Intermedio

a = [2, 5, 3, 4]
a[2:2] = [2]
print(a)
Enter fullscreen mode Exit fullscreen mode
  • A. [2, 5, 2, 3, 4]
  • B. [2, 5, 2, 4]
  • C. [2, 5, 3, 2, 4]
  • D. Error

Respuesta:

👉 A. [2, 5, 2, 3, 4]

Cuando hacemos slicing de listas y el valor de inicio es igual al valor final lo que estamos haciendo en realidad no es acceder a una parte de la lista sino apuntando a un espacio en blanco de ella, por ejemplo:

a = [2, 5, 3, 4]
x = a[2:2]
print(x) # [] -> [2, 5, apunta aqui, 3, 4]
Enter fullscreen mode Exit fullscreen mode

En nuestro ejemplo estamos asignando un nuevo valor a ese espacio de la lista a[2:2] = [2] y por ende el resultado final seria [2, 5, 2, 3, 4].


Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay