Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between an iterable and a list in python 3? [duplicate]

Under the hood, aren't iterables just lists? What's the use of having wrappers like map filter list and others like them? What does something being a filter actually tell you, except for it's origin? When would (except for being shorter in for example list comprehensions) x be better than list(x) where x is an iterable?

One thing I've heard is that keeping map and filter as iterables is faster than using them as lists. Why is this? What difference is there under the hood that makes iterables faster?

like image 699
Lara Avatar asked Oct 30 '25 12:10

Lara


1 Answers

The difference between iterable and list is like the difference between a flower and a rose.

All roses are flower, but not all flowers are rose.

All lists are iterable, but not all iterables are lists.

iterable

An object capable of returning its members one at a time. Iterables include all sequences type (like list, str, and tuples).

List is an iterable, and is having some other more specifics characteristics to list.

Check this out: https://docs.python.org/3.5/glossary.html especially on iterable part.

like image 189
Ian Avatar answered Nov 02 '25 01:11

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!