[Tutor] (no subject)

Mark Lawrence breamoreboy at gmail.com
Tue Feb 11 04:58:18 EST 2020


On 11/02/2020 00:18, Akash Mudgil wrote:
> Let's say if you wanted to search for a username in a, list where
> 'username' variable is predefined.
> 
> Example:
> 
> 
> username='BOB'
> 
> usernames=['BOB','Alice','David']
> 
> for i in range(len(usernames)):
> if usernames[i]==username:
> print("User exists in the list")
> 
> This program doea a linear search on every index in the list, and if the
> value matches, returns the output. The len function gets the total length
> of the list, and the range function defines the scope of the search from
> beginning to end of the list.
> 
> Hope this helps.
> 

Not really as it's poor python.  There is no need for the 
'range(len(usernames))' construct, simply, as has all ready been said:-

for user in usernames:
     if user == username:
        print('found)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list