0 votes
1 view
in Python by (47.8k points)

If I've got an array of strings, can I check to see if a string is in the array without doing a for loop? Specifically, I'm looking for a way to do it within an if statement, so something like this:

if [check that item is in array]:

1 Answer

0 votes
by (107k points)

For lists you can do something as follows:-

if item in my_list: 

# whatever

This works for any collection, not just for lists. For dictionaries, it checks whether the given key is present in the dictionary.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...