Genoxidus
Genoxidus

Reputation: 51

Python: Return a list

I've looked at a few of the answers for a function and the return and I'm not sure I get it. I get the return ends a function. Mine is only returning one line when there are several in the txt file.

def readList(champs):
    for i in range(len(champs)):
        champ = champs[i]
        return(str(i+93) + " - " + champ)

Under my main()

        if command == "see":
            readChamps = readList(champs)
            print(readChamps)

Intended outcome:

93 - Manchester United

94 - Manchester United

95 - Blackburn Rovers

Current outcome:

93 - Manchester United

I understand using print for def readList(champs):, but I want to understand calling the return. I could not get yield to work neither.

Upvotes: 0

Views: 96

Answers (4)

Gabio
Gabio

Reputation: 9484

You have return inside your loop, which causes the function to exit in the first iteration of the loop.

If you just want to print the lines, you can do the following:

def readList(champs):
    for i, champ in enumerate(champs):
        print(str(i+93) + " - " + champ)

and in your main:

if command == "see":
    readList(champs)

If you want to return values from readList, you should do it after you finish your loop:

def readList(champs):
    output = []
    for i, champ in enumerate(champs):
        output.append(str(i+93) + " - " + champ)
    return output

# and in your main:
readChamps = readList(champs) 
print(readChamps)

# readChamps is a list of strings. If you want to print it line by line, try:
for i in readChamps:
    print(i)

Upvotes: 2

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

Try this:-

def readlist(champs):
    champs = [str(i+93) + "-" + champs[i] for i in range(len(champs))]
    return champs

Upvotes: 1

gilch
gilch

Reputation: 11641

If you want to "return" a value without terminating the function, you can use yield instead of return. This makes the result an iterator of values instead of a single value.

def readList(champs):
    for i in range(len(champs)):
        champ = champs[i]
        yield (str(i+93) + " - " + champ)

Then to print it out, you can unpack the iterator with a *. Maybe like

print(*readList(champs), sep='\n')

Or you can use a for loop like

for line in readList(champs):
    print(line)

This will print one line at a time, instead of all at once, which may be what you want if the number of lines is large.


This approach using yield may be superior than printing directly inside of the readList() function, because it avoids side effects, which means that you can do more than just print it. You could also save the result to a file, for example.

This approach may also be better than saving it to a list and returning the whole list if the number of items is large, because it can process one at a time and doesn't have to store them all in memory at once.


And I would have written the function more like this

def read_list(champs):
    for i, champ in enumerate(champs, 93)):
        yield f'{i} - {champ}'

Standard PEP 8 style uses snake_case for function names. If you use enumerate() instead of range(), you don't have to look up the champ yourself each loop with the index. And you can start counting at 93, so you no longer have to add it. F-strings will automatically convert what they are interpolating, so you don't have to call str() yourself.

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81594

As you write yourself, return ends the function hence the loop executes exactly once.

You can use a list comprehension and enumerate:

def readList(champs):
    return ['{} - {}'.format(i, champ) for i, champ in enumerate(champs, 93)]

Alternatively, create a list, .append to it and return it in the end:

def readList(champs):
    output = []
    for i, champ in enumerate(champs, 93):
        output.append('{} - {}'.format(i, champ))
    return output

Upvotes: 2

Related Questions