11

Getting the length of reversed list doesn't work:

lst = [1,2,3]
lst = reversed(lst)
print len(lst)

throws TypeError: object of type 'listreverseiterator' has no len()

A work around is:

lst = [1,2,3]
lst_length = len(lst)
lst = reversed(lst)
print lst_length

# OR
lst = lst[::-1]
print len(lst)

Now my real question is why?
Simply reversing a list does not alter the length of the list,
so why is Python throwing that exception?

BioGeek
  • 21,897
  • 23
  • 83
  • 145
taesu
  • 4,482
  • 4
  • 23
  • 41
  • 4
    The result of `reversed` is an iterator; to determine it's length, you'd have to consume it. – tobias_k Aug 26 '15 at 13:52
  • You could check `lst.__length_hint__()` instead (although this isn't guaranteed in all Python implementations). – Alex Riley Aug 26 '15 at 13:55
  • 3
    I cannot see why you would need to check the length of a list using reversed, reversing a list is not going to change the size – Padraic Cunningham Aug 26 '15 at 13:58
  • @ajcr It was added officially in [PEP 424](https://www.python.org/dev/peps/pep-0424/) from python3.4+. An `operator.length_hint` function was added so `operator.length_hint(lst)` should work, and in fact: `>>> operator.length_hint(reversed([1,2,3])) 3` – Bakuriu Aug 26 '15 at 14:01
  • Why not just check the length first? The reversed list will necessarily be the same length as the original, right? – Karl Knechtel Aug 04 '22 at 01:19

3 Answers3

20

The function reversed() returns an iterator, not an actual list. You cannot directly get the len() of an iterator (see here). You could instead reverse the list using Extended Slice syntax:

lst_reversed = lst[::-1]

or reverse the list in place:

lst.reverse()

If you must use an iterator you could first turn it into a list:

lst_reversed = list(reversed(lst))

With each of these approaches len() will then work as expected.

Community
  • 1
  • 1
michaf
  • 342
  • 1
  • 3
  • 4
    calling list defeats the purpose of using reversed – Padraic Cunningham Aug 26 '15 at 14:18
  • does not anwer to the question (in : iterator, out : length) – Eric H. Jun 20 '18 at 09:03
  • Honestly I don't really understand this. To accurately reverse a list (or any collection/iterator) you need to iterate all of it's elements first, so that you know where to start reversing. That means the result of reversed already knows the length of what it's iterating. I don't see any harm in the implementation adding a `__len__` method to the result of reversed. The only reason I can think of is they don't want to trick people into thinking `reversed` gives a list, but IMO reversed should just be a mirror for the original array which transforms indexes. Not simply in an iterator. – Mohsin Kale Sep 23 '20 at 21:06
1

reversed doesn't produce new list, it just return iterator object. You can use lst.reverse() - and it doesn't return anything, but make your lst in reversed order

Sergius
  • 908
  • 8
  • 20
  • Note that this, contrary to the other two approaches in OP's post, will alter the list in-place, thus also reversing any other reference to that same list. Might not be a problem, just wanted to point out. – tobias_k Aug 26 '15 at 13:54
  • I see, but topic starter wants to reassign it to the same variable – Sergius Aug 26 '15 at 13:55
  • 2
    Yes, but there _might_ be _other_ references to that same list, that are _not_ meant to be reversed. In this case, using `lst.reversed()` will lead to all kinds of funny head-scratching. As I said, this might not be an issue, but worth mentioning. – tobias_k Aug 26 '15 at 13:57
1

reversed returns iterator so to determine length you have to consume it.

For example rev_len = len(list(reversed(lst))).

FYI list.reverse method reverses a list in place: lst.reverse()

Anyway your reversed list has the same size as the original one, so you can just determine the length even before reversing.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77
  • yes : transform the iterator to a list, then get the length – Eric H. Jun 20 '18 at 09:06
  • reversed(lst) returns an iterator that is "emptied" once the list() operator is applied. E.g. l=reversed([1,2,3]); print len(list(l)); for p in l:; print p --> nothing – Eric H. Jun 20 '18 at 12:24
  • reversed(lst) returns an iterator, contrarly to sorted(lst) that returns a list. – Eric H. Jun 20 '18 at 14:59