0

I have a list which I am trying to call a function on each of this list's elements (which is also a list) given a certain condition is fulfilled (the element length has to be greater than 1):

videos = [self.__process_video(e) if len(e) > 1 else '' for e in videos]

However, I am getting an error

TypeError: object of type 'generator' has no len()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Neuril
  • 183
  • 1
  • 6
  • 2
    The error has nothing to do with your use of `if`. You *don't have lists*. At least one `e` is a generator. – Martijn Pieters Feb 03 '16 at 13:11
  • 1
    It seems that `videos` contains generators, and generators do not implement the `len()` function. If `videos` is not supposed to contain generators, you need to analyze the code before that line. If it is expected, and you want to check if a generator has at least one value, this is tricky but can be done, as shown here: http://stackoverflow.com/questions/661603/how-do-i-know-if-a-generator-is-empty-from-the-start – DainDwarf Feb 03 '16 at 13:12
  • I've duped you to a post about detecting if a generator is empty, because that's the real underlying problem you are trying to solve. – Martijn Pieters Feb 03 '16 at 13:22
  • @MartijnPieters, testing for an empty generator may not be appropriate here . The OP is testing `len(e) > 1` which would be testing for more than 1 element in the generator. – Duncan Feb 03 '16 at 13:25
  • @Duncan: sure, but the same principles apply. – Martijn Pieters Feb 03 '16 at 13:25

0 Answers0