1

Here's the code I'm using:

import csv
    
my_list = ['a', 'b','c','d']
    
def listIterations(inputcsv):
    for i in inputcsv:
        x = str(i)
        return x

print(listIterations(my_list))

Here's the output: a

Here is the desired output:

a
b
c
d

What I would like is for it to iterate through the list and output each element separately.

Ultimately I'll be using this function to feed a list into another function.

petezurich
  • 9,280
  • 9
  • 43
  • 57

3 Answers3

0

In general what you are talking about is called Generator. Using a generator you could achieve that with this piece of code:

def listIterations(inputcsv):
    for i in inputcsv:
        yield str(i)

Or even shorter:

def listIterations(inputcsv):
    yield from inputcsv

For more detailed answer see this question

However in the specific case you described that can also be achieved with functional constructs.

map(lambda x: print(x), my_list)

Normally map is used to replace the values in one iterable with some other value calculated based on the input value, but in case you simply map all the value to None and as a side effect print them. Note however that this is considered a bad practice to cause side effects in functional operators like map, it pretty much contradicts the basic concepts of functional programming (thanks for @C.Nivs for pointing this out).

Daniel Lahyani
  • 925
  • 8
  • 15
  • I'd argue that using `print` inside a `map` (or `lambda`, for that matter) runs afoul of using functional style for side effects which may be better suited for an imperative style loop – C.Nivs Feb 02 '22 at 21:09
  • @C.Nivs I definitely agree, this is not functional in style. It was just for the sake of showing another option for iterating an iterable. – Daniel Lahyani Feb 02 '22 at 21:12
  • You were doing great until the `map(print, ...` part. If you're going to do that, at least don't do `lambda x: print(x)` when you can just do `map(print, ...)`. Why not just `print(*listIterator(...), sep='\n')`? – Mad Physicist Feb 02 '22 at 21:21
0

Replace return with yield to turn your function into a generator function. When you call it, it returns a generator object that you can iterate over at your leisure to produce the results you want one at a time:

def listIterations(inputcsv):
    for i in inputcsv:
        yield str(i)
>>> listIterations(my_list)
<generator object listIterations at 0x00000237062CC350>

You will only be able to use this object once. Since your input is a list, you can always call listIterations multiple times to get a new generator. To print, do a star-expand of the generator:

print(*listIterations(my_list), sep='\n')

Expanding the generator into an argument list will run it, and pass each element in to print. If you want to have a permanent record of the generated values, expand them into a list:

generated_values = list(listIterations(my_list))

or a tuple:

generated_values = tuple(*listIterations(my_list))

You can print the stashed values the same way:

print(*generated_values, sep='\n')

All that being said, if you just need an iterator over a list, you can obtain one using the builtin iter:

iter(my_list)

This is equivalent (for a list) to:

def listIterations(somelist):
    yield from somelist
listIterations(my_list)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
-1

Remember that return x means "end the function, and pass on the value 'x'" so the line return x ends your function on the loop's first iteration, and passes on whatever 'x' is. That's why you just get a, the first item in your list.

sss
  • 1
  • 1