Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

what is a iterable?

So is a iterable just really a string or list? like the = 'house' ... house would be the iterable? Thanks

Nicholas Baker
Nicholas Baker
6,964 Points

An iterable is anything that can be iterated or "stepped" through. So a string like "this" is an iterable and so is a list like ['t', 'h', 1, 's']

2 Answers

Steven Parker
Steven Parker
229,968 Points

The definition found in the Python Glossary:

Iterable

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements Sequence semantics.

An iterable is most often used to control a "for" loop.

An iterable is a data structure that can be walked through linearly (called iterating). Examples include strings, lists and tuples. As for dictionaries, you can iterate through it, but you can only iterate through either the keys or the values. (Python supports iterating both at once, true, but it's just syntactic sugar.)

In a nutshell it means that you can run a for loop through it.

I hope this helps! :zap: ~Alex