Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

How to run the function passing values from a list?

This is the list as shown below:

keywords = ['car','water','2 bottles']

and a function defined as follows:

def my_func(param):

    b = df.loc[df['Field'].str.contains(param), 'User'].tolist()

    print (f'\nThe item "{param}" was bought by in User: ', b)

    return b

Currently I am doing it manually:

buyer= my_func('car')

buyer= my_func('water')

buyer= my_func('2 bottles')

However, I would like to run a function and print results by iterating param over a list of keywords.

1 Answer

0 votes
by (36.8k points)

You can use a for loop:

def my_func(param):

    b = df.loc[df['Field'].str.contains(param), 'User'].tolist()

    print (f'\nThe item "{param}" was bought by in User: ', b)

    return b

keywords = ['car','water','2 bottles']

buyers = []

for key in keywords:

    buyers.append(my_func(key))

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Browse Categories

...