Oscar Salas
Oscar Salas

Reputation: 75

Function that takes an Int and a List

I have the below code for a card game, it should remove the N number of cars from the given card list and return a tuple with 2 lists, the first list is the N objects from the original list and the secons list is the remaining cards not extracted from the original list.

lista_cartas = [1,2,3,4,5,6,7,8]
lista_N = []
N = 5
for i in range(N):
    extracto = lista_cartas.pop(0)
    lista_N.append(extracto)
    lista_final = [lista_N, lista_cartas]
print(tuple(lista_final))

([1, 2, 3, 4, 5], [6, 7, 8])

it's working as I want but I need to trasform this into a function that takes the N number and the list as parameters, how can I achieve this?

is this somethinhg valid? or how can I make the function to take a list?

def sacar_cartas(N, lista=[]):
   for i in range(N):
       extracto = lista_cartas.pop(0)
       lista_N.append(extracto)
       lista_final = [lista_N, lista_cartas]
print(tuple(lista_final))

Upvotes: 0

Views: 172

Answers (5)

Leo Brueggeman
Leo Brueggeman

Reputation: 2521

Slightly reworked your code so that the function won't alter the passed in list (because of the lista_cartas.copy() call. As to your code and question, Python functions can accept a list as a variable without telling it that it is a list.

lista_cartas = [1,2,3,4,5,6,7,8]

def sacar_cartas(N, lista_cartas):
    lista_N = []
    lista_ct = lista_cartas.copy()
    for i in range(N):
        extracto = lista_ct.pop(0)
        lista_N.append(extracto)
        lista_final = [lista_N, lista_ct]
    return lista_final    

sacar_cartas(5, lista_cartas)

Upvotes: 1

Mario Ishac
Mario Ishac

Reputation: 5907

You can rework your solution entirely by using list slices:

lista_cartas = [1,2,3,4,5,6,7,8]

def sacar_cartas(todas_cartas, N):
    return todas_cartas[:N], todas_cartas[N:]

such that sacar_cartas(lista_cartas, 5) results in the tuple:

([1, 2, 3, 4, 5], [6, 7, 8])

Notice how we can avoid the explicit tuple call by instead returning comma-separated values.

Upvotes: 3

Tasnuva Leeya
Tasnuva Leeya

Reputation: 2795

simple conversion:

def sacar_cartas(N, lista):
    lista_N = []
    for i in range(N):
        extracto = lista.pop(0)
        lista_N.append(extracto)
    return tuple([lista_N, lista])
print(sacar_cartas(5, [1,2,3,4,5,6,7,8]))

Upvotes: 1

Maxim
Maxim

Reputation: 286

You don't necessarily have to give a list a default value of empty. You can pass a list to a function by mentioning its variable name:

lista_cartas = [1,2,3,4,5,6,7,8]
lista_N = []

def sacar_cartas(N, lista_cartas, lista_N):
   for i in range(N):
       extracto = lista_cartas.pop(0)
       lista_N.append(extracto)
       lista_final = [lista_N, lista_cartas]   
   print(tuple(lista_final))

# Call the function for it to work
sacar_cartas(N = 5, lista_cartas, lista_N)

You can define variable within the function call if you want. Thats optional because you can define it like a list before the call.

Upvotes: -1

Jeremy
Jeremy

Reputation: 671

Python is duck typed. You can pass a list in just like any other variable, without having to define the type:

def function(some_variable):
    for element in some_variable:
        print(element)

function(['1st element', '2nd element', '3rd element'])
# prints:
# 1st element
# 2nd element
# 3rd element

Doing this:

def function(some_variable=[]):

Does NOT indicate that this variable is a list and is not needed. It instead tells the function that if you do not pass this variable in, it will default to the value []

Upvotes: -1

Related Questions