[Tutor] Searching a list for a value, was Re: (no subject)

Peter Otten __peter__ at web.de
Tue Feb 11 04:59:06 EST 2020


Collisio Adolebitque wrote:

> username = 'user3'
> usernames = ['user1', 'user2', 'user3', 'user4']
> 
> 
> def check_list_for_pattern(user: str, users: list) -> bool:
>     return True if user in users else False

- Note that

    user in users  # use this

gives the same result as

    True if user in users else False  # redundant; don't use it

- I find your function name misleading. "pattern" usually denotes a regex or 
glob rather than a string that has to be matched exactly.

- I've avoided type annotations so far, but if you do it -- shouldn't users 
be marked as a list of strings? (A web search suggests List[str])

> print(check_list_for_pattern(username, usernames))

Compared to the clean and simple

print(username in usernames)

your suggestion looks a bit like overengineering ;)




More information about the Tutor mailing list