Return a list from another list in Python

Asked

Viewed 975 times

-1

Given a list of integers and an integer n, returns another list, which contains all the numbers in the original list larger than n. I am unable to return the list I need.

from typing import List
def maiores(numeros: List[int], n: int):
    for i in numeros:
        if i > n:
            return list(i)
  • This structure that you are wanting to do looks a lot like a binary search tree, as the answers are already excellent I will leave my contribution indicating a video about the binary search tree, if you want to take a look is quite enriching: https://www.youtube.com/watch?v=VmKkAQtnjsM&ab_channel=Programa%C3%A7%C3%A3oDin%C3%A2mica

4 answers

0

You can do the following:

lista = [0, 1, 2, 3]
n = 1
lista_final = []

for elemento in lista:
    if elemento > n:
        lista_final.append(elemento)

print(lista_final) # [2, 3]
  • I tried to redo using what you showed but only returns None. from Typing import List def major(numbers: List[int], n: int): larger list = [] for i in numeros: if i > n: larger.append(i) print(larger([0, 1, 2, 3], 1))

  • hello tested the execution and is all right with the code I did. try to create a new file with the end . py, copy and paste this code. Now, just go to the terminal and run: python3 filename.py

  • you can also test online, copy and paste my answer code into this online interpreter: https://www.programiz.com/python-programming/online-compiler/

0

To resolve this issue we can use the following code:

def maiores(lis, n):
    maiores_numeros = list()
    for c in lis:
        if c >= n:
            maiores_numeros.append(c)
    return maiores_numeros


valores = list(map(int, input('Digite os valores da lista: ').split()))
num = int(input('Desejas retornar os números a partir de qual valor? '))

print(maiores(valores, num))

Note that when we execute this code we receive the following message: Digite os valores da lista: . Right now we must type all the values, in the same line, separated for a single space and press enter. From this moment the list values will be mounted with all values typed.

Later we will receive the next message: Desejas retornar os números a partir de qual valor? . At this point we must enter a integer value which will represent the lower limit of the values we wish to display.

From that moment the list values and the number in a will be passed as parameters to the function major (lis, n). Getting there the block for will iterate over the list lis and, with the help of the block if verify whether the temporary value of the respective iteration - c - is higher hi equal to the value n. If yes, its value will be added to the list most and, later, such list will be displayed as function return.

OBSERVING:

Case, the entered value for the variable in a is a number that is outside the closed range of values belonging to the list values, the return of the function will be a empty list.

Testing code:

Example 1

When we execute the code and enter the following values:

2 9 3 8 4 7

And enter the amount:

5

We will receive as an exit:

[9, 8, 7]

Example 2

When we execute the code and enter the following values:

2 9 3 8 4 7

And enter the amount:

9

We will receive as an exit:

[9]

Example 3

When we execute the code and enter the following values:

2 9 3 8 4 7

And enter the amount:

10

We will receive as an exit:

[]

In this last example we will receive an empty list as a function return. This occurs because it does not exist in the list values numbers greater than or equal to 10.

  • @Marco Raad, run tests with this code.

0

You can use a function that already returns the list.

Here we define the list and what will be the value of n (this typed by the user):

lista_numeros = [1,2,3,4,5,6,7,8]
n = int(input('Valor para n: '))

We define the larger function, which uses list comprehension (generates an output list):

def maiores(lista,n):
    return [i for i in lista if i > n]

Here we call the function, save the result a variable, then use print to show the result:

lista_retorno = maiores(lista_numeros,n)
print(lista_retorno)

Exit:

[4, 5, 6, 7, 8] # o n de exemplo foi 3

0

A simpler alternative would be to filter your list through the function built-in filter(funcao, iteravel) aided by a lambda expression.

lista = [7, 9, 67, 44, 35, 14, 95, 7, 6, 78, 33]

def maiores( l , n ):
  #Para cada elemento da lista l filtra apenas os elementos maiores e iguais a n.
  return list( filter( lambda e: e >= n , l ) )

print( maiores( lista , 34 ) )

Test the code on repl it.
Upshot:

[67, 44, 35, 95, 78]

If you need to sort the result use the function built-in Sorted().

print( sorted( maiores( lista , 8 ) ) )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.