Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...