Anshika Singh
Anshika Singh

Reputation: 1044

To return elements of a list from a function

I want to define a function that takes a list as its argument and then returns the elements in order.

For example:

def iterator(lis):
    for e in range(len(lis)):
        return lis[e]

l=input("Enter list elements:").split()
x=iterator(l)
print(x)

But this just returns the first value of the list as:

Enter list elements:12 23 34
12

How can I print all the values in successive lines?

Upvotes: 0

Views: 4038

Answers (8)

Neha Adnekar
Neha Adnekar

Reputation: 1

Use of print instead of return will give you the expected output..try it once

Upvotes: -1

Pitto
Pitto

Reputation: 8589

You can use yield in order to build a generator, here's the official documentation about Generators

What is a generator in Python?

A Python generator is a function which returns a generator iterator (just an object we can iterate over) by calling yield. yield may be called with a value, in which case that value is treated as the "generated" value.

I also want to share an example, be sure to read the comments:

def iterator(lis):
    for e in range(len(lis)):
        yield lis[e]

l=input("Enter list elements:").split()

# A generator returns an Iterable so you should
# loop to print
for number in iterator(l):
    print(number)

# Or use list
result = list(iterator(l))
print(result)

Output
1
2
3
['1', '2', '3']

Upvotes: 7

PRACHI BANSAL
PRACHI BANSAL

Reputation: 11

Use: def iterator(list): for e in range(len(list)): a= list[e] print(a) l=a,b,c=input().split() a=iterator(l)

Upvotes: 1

Shaji James
Shaji James

Reputation: 16

def iterator(lis):
    for e in range(len(lis)):
        print( lis[e])

l=input("Enter list elements:").split()
iterator(l)

if you want to do some operations for each item in the list, then you should accomodate those within the function.

Upvotes: 0

Thaddaeus Markle
Thaddaeus Markle

Reputation: 534

return causes the function to stop after it hits the statement. So your for loop only ever runs once.

You could use yield as mentioned in the other answers, but I really don't think you need a function in this situation. Because the function is just going to return the list that it took as an argument. What you should do is something like this:

i = input("Enter list elements: ").split()
for x in i:
   print(x)

It's that simple.

Upvotes: 0

Vishal Dhawan
Vishal Dhawan

Reputation: 351

It will print only one element if you do return

def iterator(lis):
    for e in range(len(lis)):
        return lis[e]
l=input("Enter list elements:").split()
x=iterator(l)
for y in x: print(y)

Upvotes: 1

Sanket Singh
Sanket Singh

Reputation: 1366

Use:

[print(i) for i in input("Enter list elements:").split(" ")]

Upvotes: 0

chepner
chepner

Reputation: 532268

You probably want yield, as return causes the function call to end immediately. But yield just produces another iterator; you still need to iterate over the result to print them all, rather than simply printing the iterator itself.

def iterator(lis):
    for e in range(len(lis)):
        yield lis[e]

...
for element in x:
    print(element)

Of course, you are pretty much just reimplementing the existing list iterator here; an equivalent definition would be

def iterator(lis):
    yield from lis

What you might want instead is to do something like

x = '\n'.join(l)
print(x)

which creates a string by iterating over l and joining the elements using \n. The resulting multiline string can then be printed.

Upvotes: 1

Related Questions