0

If I have lists like these:

transactions = ['001','002','003']
transaction_dates = ['01-01-2019','01-02-2019','01-03-2019']
transaction_amounts = ['27.00','35.00','36.00']

And I use zip like this

results = zip(transactions,transaction_dates,transaction_amounts)

How do I tell if results is empty?

yatu
  • 80,714
  • 11
  • 64
  • 111
Reez0
  • 2,112
  • 2
  • 19
  • 34
  • 1
    But `results` will never be empty in this case. The only case where `results` can be "empty" (in quotes because it will never be "empty", but an empty generator) is if at least one of the lists is empty. – DeepSpace Jul 09 '19 at 11:02
  • 1
    Give us example data that would make `results` empty? – buhtz Jul 09 '19 at 11:03

1 Answers1

2

For this specific task I would suggest defining a function and calling the next method of the iterator returned by zip wrapped with a try/expect to catch the StopIteration warning:

def is_empty(i):
    try:
        next(i)
        return False
    except StopIteration:
        return True

transactions = ['001','002','003']
transaction_dates = ['01-01-2019','01-02-2019','01-03-2019']
transaction_amounts = ['27.00','35.00','36.00']

is_empty(zip(transactions,transaction_dates,transaction_amounts))
# False
yatu
  • 80,714
  • 11
  • 64
  • 111