Python Forum
returning values in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
returning values in for loop
#1
in my example i would like to return the whole list with it being in a function instead i am only getting 1 as a return.
However when its not in a function i will get the full list. So my question is how can i put this for loop in a function and have it return all the values if the function is called.

def example():
    list_1 = [1, 2, 3, 4, 5]
    for i in list_1:
        return i

print(example())
Reply
#2
Use yield instead of return. This turns example() into a generator, like range(). Then you could do this:
for x in example():
    print(x)
# or
y = list(example())
If you really just want to return the list. Just return the list.
def example():
    return [1, 2, 3, 4, 5]
bowlofred likes this post
Reply
#3
A function can only return one object. If you want multiple values, you have to return it in a collection object of some sort (list, tuple, set, dict, etc.) In your example, you could return the list directly, or you could return the data in some other object with a copy of the data.

Not sure what is intended by "when not in a function i will get the full list". You can print out multiple things to the screen, so maybe that's what you mean? If you show the code, that might make more sense.
Reply
#4
Return statement exits the function. So when it returns 1 from the list, it also exits the function.
Reply
#5
Here some examples with yield which as mention make it generator,and doing something to list so it make some senseđź‘€
def example(example):
    for i in list_1:
        yield i ** i

list_1 = [1, 2, 3, 4, 5]
print(example(list_1))
print(list(example(list_1)))
for i in example(list_1):
    print(i)
Output:
<generator object example at 0x03EE8D30> [1, 4, 27, 256, 3125] 1 4 27 256 3125
A generator has a lot power and will take up memory/process(lazy-evaluation) only when it's called.
So if eg itertools.islice can eg calculate only part of result,then power is only used for this parts and not rest of the list.
from itertools import islice

def example(example):
    for i in list_1:
        yield i ** i

list_1 = [1, 2, 3, 4, 5]
print(list(islice(example(list_1), 2 ,4)))
for i in islice(example(list_1),1, 3):
    print(i)
Output:
[27, 256] 4 27
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through values and compare edroche3rd 6 684 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,095 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,481 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,219 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 1,901 Mar-27-2022, 07:58 PM
Last Post: deanhystad
  How to add for loop values in variable paulo79 1 1,441 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  For Loop Returning 3 Results When There Should Be 1 knight2000 12 4,090 Sep-27-2021, 03:18 AM
Last Post: SamHobbs
  Returning values from Gaussian fitting Laplace12 0 1,570 Aug-05-2021, 08:09 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

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