Valentyn
Valentyn

Reputation: 612

How to get all values from a worksheet with a filter using gspread python lib?

Here is the way to get all values from a worksheet as a list of lists:

list_of_lists = worksheet.get_all_values()

But how to get all values from a worksheet with a filter?

For example, I have IS_ACTIVE column in the worksheet. So I need to get values only where IS_ACTIVE=1.

Upvotes: 1

Views: 1932

Answers (1)

zerocool
zerocool

Reputation: 318

I think your best bet would be to sort the data after you've scraped the worksheet. It might be a slower approach, but it'll be straight forward at the least.

list_of_lists = worksheet.get_all_values()
filtered_list = []
for i in list_of_lists:
    if i[<index of IS_ACTIVE column>] == 1:
        filtered_list.append(i)

If you're familiar with functional programming and you want to be fancy you can use the built in filter method with a lambda. Docs here.

list_of_lists = worksheet.get_all_values()
filtered_list = list(filter(lambda i: i[<index of IS_ACTIVE column>] == 1, list_of_lists))

Upvotes: 2

Related Questions