DEV Community

Cristian Fernando
Cristian Fernando

Posted on

1

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

Explica este código Python

Dificultad: Fácil

my_list = [1, 2, 3]
my_list.append([4,5])
print(my_list)
Enter fullscreen mode Exit fullscreen mode
  • A. [1, 2, 3, 4, 5]
  • B. [1, 2, 3, [4, 5]]
  • C. [1, 2, 3, (4, 5)]
  • D. [1, 2, 3, "45"]

Respuesta:

👉 B. [1, 2, 3, [4, 5]]

El método append() sirve para agregar elementos al final de una lista, ojo, elementos sueltos; en nuestro ejemplo intentamos agregar otra lista.

Es posible hacer esto con append() pero el resultado no sera esperado por que tendremos una lista anidada.

En este caso, podríamos usar el método extend() que nos regresará [1, 2, 3, 4, 5].

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

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