Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 07 · Listas · Lesson 05

Buscar en una lista con in

in también pregunta si un valor es un elemento de la lista.

Lesson content is in Spanish.

Python in your browser

The first time, Python downloads about 5 MB (only once; then it stays saved). If you can, use Wi-Fi.

Python is already saved in this browser: it will not download again.

Python downloads when you start reading.
  1. 01Understand
  2. 02Try
  3. 03Explain

The situation

Una tienda mantiene una lista de los productos que no tienen stock. Cuando alguien busca un producto, el sistema tiene que saber enseguida si está en esa lista.

Understand each piece

  • valor in lista

    Da True si algún elemento de la lista es exactamente igual al valor. "monitor" in ["monitor", "webcam"] da True.

  • not in

    Da lo contrario: True si el valor no está en la lista.

  • Igualdad exacta

    Con listas, in compara elementos completos: "moni" in ["monitor"] da False, porque ningún elemento es exactamente "moni".

Example

sin_stock = ["monitor", "webcam"]
print("monitor" in sin_stock)
print("teclado" in sin_stock)

monitor es uno de los elementos, así que da True. teclado no está, así que da False.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
sin_stock = ["monitor", "webcam"]
print("Monitor" in sin_stock)

Your mission

¿Hay stock?

La prueba crea la lista sin_stock (productos en minúsculas, puede estar vacía) y la variable producto. Si el producto está en sin_stock, mostrá monitor: sin stock. (con el nombre del producto). Si no, mostrá teclado: disponible. (con el nombre del producto).

  1. if producto in sin_stock:
  2. Adentro: print(f"{producto}: sin stock.").
  3. else: y adentro print(f"{producto}: disponible.").

If you get stuck

One hint at a time: the first points the way and the last almost solves it.

The key idea

valor in lista pregunta si algún elemento es exactamente igual al valor.

Solve with help

If you have already tried, you can see a reference solution. The lesson is marked as solved with help and adds no XP.