You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
14 Hrs
26 Min
23 Seg

Playgrounds: Agrega solo los números positivos de una lista

35/38

Contributions 492

Questions 9

Sort by:

Want to see more contributions, questions and answers from the community?

Primer comentario y aporte en todo lo que llevo de Platzi, buena estrategia esta de los playground.

si alguien se pregunta como hacerlo con un whiel

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

Escribe tu solución 👇

i = len(my_list)
counter = 0
while counter < i:
if my_list[counter] > 0:
new_list.append(my_list[counter])
counter += 1
print(new_list)

SIUUUUUUU

for element in my_list:
  if element > 0:
    new_list.append (element)

print(new_list)

Aqui mi solucion:

lista = [1,2,3,4,-8,-9,15,44,-896]
new_lista= []
for e in lista:
  if e < 0:
    continue
  else:
    new_lista.append(e)

print(new_lista)

# [1, 2, 3, 4, 15, 44]

Recuerda colocar el print() en todo el comienzo de la línea.

Hola, les comparto mi versión de una solución del playground, luego de varios intentos fallidos 😅

new_list = [num for num in my_list if num >= 0]

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇

for i in my_list:
    if i >= 0:
        new_list.append(i)
print(new_list)

asi que chiste , si todos ponen sus respuestas

Genial los Playgrounds!

Solución:

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇
for number in my_list:
  if not number < 0:
    new_list.append(number)

print(new_list)

for i in my_list:
if i > 0:
new_list.append(i)
print(new_list)

![](https://static.platzi.com/media/user_upload/image-950d77ce-d8c4-4999-837b-4fd3057f12b7.jpg) Solucion alterna usando list comprehension

Mi manera de resolverlo, no se si sea la más correcta pero funciona

for i in  range(len(my_list)):
    break
my_list.sort()
new_list = print(my_list[4:])

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

for number in my_list:
if number >= 0:
new_list.append(number)

print(new_list)

Que buen ejercicio

Mi solución!!

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

Escribe tu solución 👇

for element in my_list:
if element >0:
new_list.append(element)

print(new_list)

Este es mi código para depurar la lista original y que la nueva
lista sólo sea de números positivos:

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇

for element in my_list:
  if element > 0:
    new_list.append(element)
    
print(new_list)

La imagen de la corrida se muestra a continuación:

![](

Una solución posible para el ejercicio:

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇
for number in my_list:
  if number>0:
    new_list.append(number)

print(new_list)

for new_list in range (1,5):
new_list = [1, 2, 3, 4]
print(new_list)

HAsta que por fin lo logre errores que cometo por no ser detallista.

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇
for i in my_list:
  if i > 0:
    new_list.append(i)

print(new_list)
# declaración e inicialización de variables
my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇
#ciclo
for number in my_list:
  if number > 0:
    new_list.append(number)
# output
print(new_list)

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

Escribe tu solución 👇

for i in my_list:
if i > 0:
new_list.append(i)
print(new_list)

Solucionado

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []
for i in my_list:
if i > 0:
new_list.append(i)
print(new_list)

Mi solucion

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

Escribe tu solución 👇

for element in my_list:
if element > 0:
new_list.append(element)
print(new_list)

for number in my_list:
if number > 0:
new_list.append(number)
print (new_list)

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

Escribe tu solución 👇

for i in my_list:
if i > 0:
new_list.append(i)

print(new_list)

my_list = [1, -1, 2, -2, 3, -3, 4, -4]
new_list = []

# Escribe tu solución 👇

for positivos in my_list:
  if positivos >= 0:
    new_list.append(positivos)
print(new_list)

![](https://static.platzi.com/media/user_upload/upload-5b4ae058-67b8-48dd-91f4-dd2bb1e7bf27.png)
![](https://static.platzi.com/media/user_upload/image-002bf727-98f3-4d3e-be32-aadc898ab22a.jpg)
my\_list=\[1,-1,2,-2,3,-3,4,-4] new\_list=\[] for number in my\_list: if number > 0: new\_list.append(number) print(new\_list)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if element>0: new_list.append(element) continue else: continue print(new_list) ```my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇 for element in my\_list: if element>0: new\_list.append(element) continue else: continue print(new\_list)
Diria que la mas eficiente es esta: ```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [postitivos for positivos in my_list if positivos >= 0] print(new_list) ```
```python my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [positivos for positivos in my_list if positivos >=0] # Escribe tu solución 👇 print(new_list) ```my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[positivos for positivos in my\_list if positivos >=0] \# Escribe tu solución 👇 print(new\_list)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4] new\_list = \[] \# Escribe tu solución 👇 for positive in my\_list: if positive > 0: new\_list.append(positive) print(new\_list)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4] new\_list = \[] \# Escribe tu solución 👇 for positive in my\_list: if positive > 0: new\_list.append(positive) print(new\_list)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4] new\_list = \[] \# Escribe tu solución 👇 for positive in my\_list:  if positive > 0:    new\_list.append(positive) print(new\_list)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇for positive in my\_list:  if positive > 0:    new\_list.append(positive) print(new\_list)
for x in my\_list:  if x > 0:    new\_list.append(x) print(new\_list)
![](https://static.platzi.com/media/user_upload/codigo-9bf2d4fb-d626-4f49-bc17-d75bc5084690.jpg)
Dejo mi solución: ![](https://static.platzi.com/media/user_upload/2024-06-24_16h12_39-db7b0acd-0545-4e3b-898d-5a749b8c327d.jpg)
Mi solucion: ![](https://static.platzi.com/media/user_upload/image-f102914f-658a-4bdf-b8c1-348912dd25f0.jpg)
![](https://static.platzi.com/media/user_upload/image-faa7f590-06ce-49fc-85ba-4aed083daa70.jpg)
Yay!! ![](https://static.platzi.com/media/user_upload/image-8ee9c4f1-5d87-4335-b069-118d213f335e.jpg)
`my_list = [1, -1, 2, -2, 3, -3, 4, -4]` `new_list = []` `for i in my_list:` `if i >= 0:` `new_list.append(i)` `print(new_list)`
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4] new\_list = \[] for i in my\_list: if i >= 0:  new\_list.append(i) print(new\_list)
.. /\\.../\\. \=๑•ᴗ•๑ = . (./.||. \\.) ¸.•\* .. /\\.../\\. \=๑•ᴗ•๑ = . (./.||. \\.) ¸.•\* .. /\\.../\\. \=๑•ᴗ•๑ = . (./.||. \\.) ¸.•\* ```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for i in my_list: if i > 0: new_list.append(i) print(new_list) ```
**Mi solución:** ```python my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] new_list = [item for item in my_list if item > 0] print(new_list) ```
como ejercicio de curiosidad, cambien la identación (alineación) de la línea 8 y vean los resultados para comprender qué sucede! ![](https://static.platzi.com/media/user_upload/image-00e66949-48a6-43fc-a842-d20b3922cfae.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if element < 0: continue new_list.append(element) print(new_list) ```
![](https://static.platzi.com/media/user_upload/image-caffe6a4-3e63-442f-9d8a-dd13431415c1.jpg)![](https://static.platzi.com/media/user_upload/image-d1ba2173-c962-4be7-9b5f-3860ee2ea0fd.jpg)
Mi solucion es lo mas feo de lo feo pero funciono XD ```js numero = my_list[::2] new_list += numero print(new_list) ```
![](https://static.platzi.com/media/user_upload/image-6e3e0de0-7c12-45a0-8051-a31386a58885.jpg)![]()
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇 print(new\_list) for n in my\_list: if (n % 2) == 0: new\_list.append(n) print(new\_list)
![](https://static.platzi.com/media/user_upload/image-554c1abe-639c-4640-9324-bdb735cc7668.jpg)
![](https://static.platzi.com/media/user_upload/image-41db6fba-9ada-4730-b692-9528481a1d2f.jpg)
![](https://static.platzi.com/media/user_upload/Screenshot%202024-05-21%20at%2010.10.21%E2%80%AFAM-11c55c42-4434-4beb-8d82-ee9d65f4b872.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 def filtrar_lista(my_list, new_list): for number in my_list: if number > 0: new_list.append(number) filtrar_lista(my_list, new_list) print(new_list) ```
`my_list = [1, -1, 2, -2, 3, -3, 4, -4]` `new_list = []` `# Escribe tu solución 👇def filtrar_lista(my_list, new_list):  for number in my_list:    if number > 0:      new_list.append(number)` `filtrar_lista(my_list, new_list)` `print(new_list)`
![](https://static.platzi.com/media/user_upload/Captura%20de%20Pantalla%202024-05-15%20a%20la%28s%29%2022.58.04-26cffa5d-c4b3-4811-967e-dae7bb00b7b6.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for positivo in my_list: if positivo>0: new_list.append(positivo) print(new_list) ```
**Solucion:**my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] for element in my\_list: if element > 0: new\_list.append(element) print(new\_list) ```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] for element in my_list: if element > 0: new_list.append(element) print(new_list) ```
Alguien sabe por qué este código no sirve para el ejercicio? ```python my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if my_list[element] > 0: new_list.append(element) print(new_list) ```Me imprime lo siguiente: ```python [2, -2, 4, -4] ```
```python my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if my_list[element] > 0: new_list.append(element) print(new_list) ``` ```js hjkhjkhj ```
![](https://static.platzi.com/media/user_upload/image-39d15376-1a82-4d4d-a756-9b2fc63e79a5.jpg)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇for i in my\_list:  if i >0:    new\_list.append(i) print(new\_list)
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4] new\_list = \[] \# Escribe tu solución 👇 for num in my\_list:  if num >= 0:    new\_list.append(num) print(new\_list)
Consolido en este mensaje mis aprendizajes nuevos y los significativos. 1. El ejercicio cuenta con una lista, la lista es identificable por \[ ] los corchetes. 2. Como es una lista, y está definidia, es decir, cuenta con unos valores específicos, se puede emplear el loop ***for*** para eso. 3. Lo que ***for*** hace es realizar un recorrido por cada uno de los elementos y los desgloza o impirme en la terminal. 4. Esa "acción" de recorrer cada elemento de la lista, tupla, diccionario... se delcara en el for a través de ***<u>element, </u>**aunque se le puede asignar cualquier nombre***,** para el caso del ejercicio este fue ```js for number in my_list: ``` 1. Se puede dar una condición para que se cumpla como if. 2. En el ejecicio, se requerría crear una nueva lista a partir de ese if, y que esto quedara declarado en new\_list. Para eso se empleó la opción ***<u>.append()</u>***. No recordaba su función. Me ha parecido muy útil.
![](https://static.platzi.com/media/user_upload/Screenshot%202024-04-15%20145112-faa83fc9-681c-4c25-a17a-21461c89d9fb.jpg)![](https://static.platzi.com/media/user_upload/Screenshot%202024-04-15%20145112-04a537f6-ce6a-4bb3-984a-4cde4689f6c1.jpg)
```js for n_positive in my_list: if n_positive > 0: new_list.append(n_positive) print(new_list) ```
![](https://static.platzi.com/media/user_upload/image-0a750799-6749-440a-92e3-8d3648cfe3a3.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for i in my_list: if i >=0: new_list.append(i) print(new_list) ```
my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇for i in my\_list:  if i >=0:    new\_list.append(i) print(new\_list)
Se puede solucionar así también y dejando ambos prints en la parte finalfor element in my\_list:  if element > 0:    new\_list.append(element)print(my\_list)print(new\_list) ```python for element in my_list: if element > 0: new_list.append(element) print(my_list) print(new_list) ```
![](https://static.platzi.com/media/user_upload/image-8516c1d3-002e-4541-bc87-da3cd5114252.jpg)
for number in my\_list: if number > 0: new\_list.append(number) print(new\_list)
Mi solución my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇for numeros in my\_list: if numeros > 0: new\_list.insert(numeros,numeros) print(new\_list)
![](https://static.platzi.com/media/user_upload/image-814ccc59-6d05-45b7-a899-4228f2fd4c63.jpg)
![](https://static.platzi.com/media/user_upload/image-c9a92d4f-eb6b-44c2-b803-26fb7bfb2cb6.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if element > 0: new_list.append(element) print(new_list) ```
![](https://static.platzi.com/media/user_upload/image-0409b681-c94b-4098-8d06-b27dc163f968.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for elemento in my_list: if elemento > 0: new_list.append(elemento) print(new_list) ```my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇for elemento in my\_list:  if elemento > 0:    new\_list.append(elemento) print(new\_list)
![](https://static.platzi.com/media/user_upload/image-0c96894c-e2b2-494e-a323-d15929454964.jpg)
new\_list =\[x for x in my\_list if x>0]
```python my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for num in my_list: if num > 0: new_list.append(num) print(new_list) ```
my\_list = \[ 1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] for listado in my\_list: if listado > 0: new\_list.append(listado) \# Escribe tu solución 👇 print(new\_list)
`<` `my_list = [1, -1, 2, -2, 3, -3, 4, -4]` `new_list = []` `for number in my_list:` ` if number > 0:` ` new_list.append(number)` `print(new_list)` `>`
![](https://static.platzi.com/media/user_upload/image-869f1fb3-f201-4a0f-a434-79355ed82db7.jpg)
![](https://static.platzi.com/media/user_upload/image-3b6bbe5e-4d6a-4180-9499-2d7fdc1500b7.jpg)
![](https://static.platzi.com/media/user_upload/image-73dc9da9-6e88-4cb0-a805-7a8dc162f599.jpg)
```js my_list = [1, -1, 2, -2, 3, -3, 4, -4] new_list = [] # Escribe tu solución 👇 for element in my_list: if element > 0: new_list.append(element) print(new_list) ```my\_list = \[1, -1, 2, -2, 3, -3, 4, -4]new\_list = \[] \# Escribe tu solución 👇 for element in my\_list:  if element > 0:    new\_list.append(element)print(new\_list)
Aquí está la solución que he realizado para este reto 💻👨‍💻 ![](https://static.platzi.com/media/user_upload/image-06565971-64c6-4b21-ab70-002b801e016c.jpg)
undefined