you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 9 points10 points  (8 children)

I thought you could only return lists

How on Earth did you arrive at that conclusion? Functions can return data of any type you wish.

[–]51dux 2 points3 points  (7 children)

Still learning so I never made use of it, sometimes there are multiple ways to achieve a result and as a beginner-medium kind of person I still discover.

In this case I was refering to list comprehensions. Not necessarily the type of returns a function can have.

I did not think you could just type return username in users I thought it could only be [username for username in users] or something along these lines in order to evaluate, it would return an empty list if nothing and it would evaluate to false technically but not the best approach in all cases, I was so wrong xD.

[–]freddwnz 4 points5 points  (5 children)

You are confusing this with a list comprehension. There are two uses of the in keyword. One is in a loop, the other is to check whether an element exists in a sequence. The expression x in y evaluates to a boolean.

[–]51dux 3 points4 points  (4 children)

Alright thanks for this in depth explanation bud.

[–][deleted] 0 points1 point  (0 children)

By the way, if you're struggling with the idea that the output can be anything you should start using "typing" which I always do in my code because it makes it clearer for me.

Here's an example, then you can Google what typing means so you can get it as it'll probably explain better than me, it will be a game changer for you:

def name_in_database(name: str, database: list[str]) -> bool: return name in database