TypeError: argument to reversed() must be a sequence

ArekBulski picture ArekBulski · Nov 30, 2015 · Viewed 8k times · Source

How come enumerate does not produce a sequence?

----> 1 BytesInt('1B')

     12 def BytesInt(s):
     13     suffixes = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']
---> 14     for power,suffix in reversed(enumerate(suffixes)):
     15         if s.endswith(suffix):
     16             return int(s.rstrip(suffix))*1024**power

TypeError: argument to reversed() must be a sequence

Answer

Daniel Roseman picture Daniel Roseman · Nov 30, 2015

enumerate indeed does not return a sequence, it is a generator. If your input is relatively small you can convert it to a list:

for power, suffix in reversed(list(enumerate(suffixes))):