Python Forum
How to append to list a function output?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append to list a function output?
#3
You're not returning anything. Also in the for loop replace that i with "_" as you're not using that variable and it would interfere with the one you defined before hand. The print function will only send the value to the console to display it. Also if you plan on adding all those values into the list, then you'll have to first put all those values into a list inside the function, and then return the list. You can then define arr as arr = output(10) but if you want to append the list of numbers to arr then you can change them to tuples and add them together, since lists cannot be added together list(tuple(arr)+tuple(output(10))). Though if you're just trying to get a list of numbers in a certain range you can just do list(range(0, 10)). Now since you probably don't know what the return is, I'll give an example:
import random

def add_two_random_numbers():
    return random.randint(1, 5) + random.randint(1, 5) # random.randint(1, 5) return a random number between 1 and 5. They are then added together and returned

def return_two_random_numbers():
    return random.randint(1, 5), random.randint(1, 5) # You can also return two or more values through commas


def return_nothing():
    return # Someimes you want to use a return function just to exit out of the function and return back
    print("returned") # Which is why anything after the return function will not execute

print(add_two_random_numbers())
number1, number2 = return_two_random_numbers() # You can store the return values this way if you'd like
numbers = return_two_random_numbers() # Or you can also store them this way, in which they will be returned as a tuple
print(number1, number2, numbers)
print(return_nothing())
Output:
5 3 2 (4, 4) None
Reply


Messages In This Thread
RE: How to append to list a function output? - by SheeppOSU - Aug-23-2020, 07:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 462 Mar-14-2024, 06:26 PM
Last Post: flash77
  problem in output of a function akbarza 9 1,178 Sep-29-2023, 11:13 AM
Last Post: snippsat
Question How to append integers from file to list? Milan 8 1,444 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  How to print the output of a defined function bshoushtarian 4 1,278 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,343 Jun-12-2022, 06:54 PM
Last Post: Mark17
  sum() list from SQLAlchemy output Personne 5 4,454 May-17-2022, 12:25 AM
Last Post: Personne
  output correction using print() function afefDXCTN 3 11,068 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,206 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,105 Feb-12-2021, 09:04 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020