Olivier Turcotte Olivier Turcotte - 2 months ago 10
Python Question

Python list out of function

I Need

['1', '2', '3']
to become this
[1, 2, 3]


This is my actual code:

def chain_a_list_int(p_chain :str):
tab_chain=[] # [str]
tab_int=[] # [int] (list to return)

tab_chain = p_chain.split(",")
tab_chain = [int(i) for i in tab_chain]
tab_int.append(tab_chain)
return tab_int


and return it to use it like that:

chain_a_list_int(input("enter the number to conserve: "))


but it give me this error when i print it out of the function:

<function chain_a_list_int at 0x000000000349FEA0>


when i try to print i use this:

print(chaine_a_liste_entier)


when i print
tab_int
in the function it work pretty well but it's not when i'm out of it...

print(tab_int)


Result...

[1, 2, 3]

cxw cxw
Answer

That's not an error, but an indication that Python doesn't think that you have asked it to call chain_a_list_int. The minimal tweak to your code is:

the_list = chain_a_list_int(input("enter the number to conserve: "))
print(the_list)

or

print(chain_a_list_int(input("enter the number to conserve: ")))

A reference to the name of the function chain_a_list_int, without a ( after it, does not actually cause the function's code to run. This distinction will be useful to you later on — for now, make sure any time you type the name of a function, you put a parenthesized expression after that name. (If @ForceBru posts an answer, you'll see a counterexample :) .)