check if there is a string in your dataset in python

To check if a string is present in a dataset in Python, you can use the in operator or the find() method. Here's an example:

main.py
dataset = ['apple', 'banana', 'orange', 'grape']
query = 'banana'

# Using the 'in' operator
if query in dataset:
    print(f"{query} is present in the dataset")
else:
    print(f"{query} is not present in the dataset")

# Using the 'find()' method
if dataset.find(query) != -1:
    print(f"{query} is present in the dataset")
else:
    print(f"{query} is not present in the dataset")
385 chars
15 lines

In the above code, the in operator is used to check if the query string is present in the dataset list. If it is, it prints that the string is present; otherwise, it prints that the string is not present.

Alternatively, you can use the find() method on the dataset string to check if the query string is present. The find() method returns the index of the first occurrence of the query string in the dataset string. If the query string is not found, it returns -1.

Remember to replace dataset with your actual dataset and query with the string you want to check.

related categories

gistlibby LogSnag