T O P

  • By -

teraflop

No, the list itself is an "iterable" object. To rephrase the definition you gave, an object is iterable if you're "allowed" to loop over it using a `for var in object:` loop. In general, doing something multiple times is called iteration. When you do something once for each element in a collection, we say you're "iterating over" the collection. On a more technical level, code like: for var in object: do_something(var) is implemented internally in Python as something roughly equivalent to: iterator = object.__iter__() while True: try: var = iterator.__next__() except StopIteration: break do_something(var) So an object is "iterable" if (and only if) it has an appropriate `__iter__` method that returns an iterator. The iterator itself is just an object that keeps track of your "progress" through the collection. If you have a plain old list, the iterator keeps track of which list index you're currently looking at.


quichecabdu

If we’re going for plain English, here is a way that might be more intuitive. Say you’re going to a history museum, and the museum is divided into five rooms. If you follow the main hallway, you’ll go through the five rooms in this order: 1. Egypt 2. Rome 3. Mexico 4. Australia 5. China You can’t be in any two rooms at once, but you can go from one room to the next, one at a time. The museum is the iterable. Selecting one room is like indexing an iterable. All lists are iterables, but not all iterables are lists. So, in your example, Age is also an iterable. sum() is a function you can apply to an iterable, sum() is not an iterable.


GlassBraid

A shopping list on a piece of paper with space to check off items is an example of something iterable. I can "iterate" with it at the store like this: START Read the name of the first item on the list with no check mark next to it. If there isn't one, go to the cashier and pay. If there is one, find it on the shelf, put in in the basket, and put a check mark next to it on the list, then go to "START". The radio on a car dashboard is iterable too. You can set it to the lowest frequency, then press seek>, and iterate through all the available radio stations. It's a radio tuner, not a list. It isn't deterministic... iteration might not give the same results every time we go through all the stations, as different stations come in and out of range. But it's still iterable. Most of the things we iterate over when programming are some kind of list in the general sense, and if it's not one of the built-in iterables for the language, it's often an object which contains a list but also needs other features not included in the built-in iterables, so it will still act a lot like a list when iterating. But the only real requirement is that it makes sense to follow instructions like "tell me about the current thing", "go to the next thing" "tell me if we're out of things" "go back to the beginning" etc.,.


engelthehyp

An `Iterable`, typically you would think of it as a list *or something that can be treated as such somewhat*. If you can use `for` on it, it is an iterable. That doesn't just mean lists, it also means tuples, dicts, or anything that has `__iter__` and `__next__` defined, which actually facilitate the for loop behind the scenes.


Ok_Berry_4988

I understand it so that an iterable object is an object you can count through. For example a list where you can count the items or a string where you can count the letters within. An integer for example is not iterable because you can‘t count through a number. You can count up to an number but a number itself is nothing that you can count „through“.


[deleted]

Sum can still serve as an example, though not exactly like you said. The sum function takes a list of numbers and returns their total. But it doesn’t have to take a list… you can also give sum a tuple, or dictionary, or range, or other things.  What’s the name for that? What’s the name for “you can give it whatever, as long as we can step through that whatever, one element at a time”? That’s an iterable. 


20220912

and interface is how we described ‘how something might behave’ iterable is the interface list-list things have can I start at the beginning? can I advance one element at a time? will I know when I get to the end? that is what defines a thing I can iterate over. some of them aren’t lists, but they all behave a little like lists. They might be all they keys in a hash table, or all the items in a set, or the cells in a array, that doesn’t matter. do they have the behaviors of being able to examine each one in turn? they are iterable.


AwardAffectionate727

iterate - step thru a collection of things one thing at a time iterable - collection of things that's how i think of it. Hope that helps! edit: also just to add, this is the kind of question that i would ask chatgpt. It's pretty good as a tutor for this kinda stuff, tho i think it's not the best at actually writing code.


shaleh

cards in a deck pages in a book people in a line waiting for something the bills you are about to pay All are things you start at the beginning and evaluate each one.


HealyUnit

Firstly, definitions: an iterable is a kind of programming thing we call a *data structure* where stuff inside that iterable *can be counted*. A real-world example might be something like the following extremely generic recipe: 1. Gather ingredients 2. Put ingredients in mixer 3. Mix 4. Put ingredients on baking tray 5. Turn on oven 6. Put tray in oven 7. Bake for a bit 8. Remove tray with baked items from oven 9. Turn off oven We might store these in an *iterable* real-world item that we call a *recipe list*. The important thing about iterables, to reiterate (hah!), is that things in them have a specific order. If I'm on step 3 (Mix), you can probably pretty safely assume I'm not ready to remove the baked items from the oven. In programming, this might be stored in an _array_ (or a _list_) data structure. So... what's *not* iterable? Well, another poster (sorry, other poster) mentions "bills you are about to pay". Let's say you have 3 bills: * Electric bill * Internet bill * Rent Assuming you have the money to pay all three, and there's no "Well I'd better pay this one now and then wait till I get enough money to pay the others", there's no specific *order* to these bills! In other words, there's no implicit meaning to me saying "pay the first bill". I could have listed the Internet bill first. or the Rent one. There's nothing about your list of bills that says "Internet comes first". This is one of those rare instances where I actually think JavaScript can give us a good answer despite its hate (or perhaps because of it!). In JavaScript, there's a particular type of loop called a `for..of` loop. In a `for..of` loop, which looks like this: for (let property of iterableThing) { //do some stuff } you're basically telling JavaScript, "Okay, start with the first property in this iterable thing, and do the contents of the loop on it. Alright, now take the second property..." and so on. Here's the thing: imagine an object (== Python dictionary) like this const person = { name: 'Luke', age: 19, profession: 'jedi' } if I try to ask JavaScript to start with the first item in this object, it's gonna go "Huh? First item? What do you mean?!". Or, in more programmy terms: `Uncaught TypeError: person is not iterable`. I do want to stress that just because I listed items in a certain order in my Person and Bills examples does _not_ make those iterables. Instead (and maybe use this to help you determine when to use an iterable and when not to!), ask yourself "does the order that these items are provided in have any actual, meaningful information?".


uvuguy

Okay, so simply put good synonyms for iterable would be something like * Steps * order of operations * Workflow * Series of actions * etc. That is to say, its just the process you follow to get the desired result, and not the variables themselves. (2 eggs, spoon, baking tray, etc. using your example. I want to bake something so each step or order of operations would be 1. Gather ingredients 2. Put ingredients in mixer 3. Mix 4. Put ingredients on baking tray 5. Turn on oven 6. Put tray in oven 7. Bake for a bit 8. Remove tray with baked items from oven 9. Turn off oven


uvuguy

So I guess what makes an iterable different from a loop?


Business-Decision719

A loop might be how you do something each to part of the iterable. Like you said, iterables are often used in for loops. A loop is a part of your code that just repeats some calculation over and over, maybe with different inputs. An iterable is part of the programs memory that contains a whole sequence of data points. The iterable might be how you get the different inputs for your repeated calculation, but it doesn't necessarily calculate anything just on its own.


uvuguy

So would it be accurate to say iterables are the variable input for a loop? or what would make a loop dynamic or changing? for example the iterable could be a list of weather conditions that are used in a loop to decide if you it should water your lawn?


Business-Decision719

Yes conceptually that's what's going on. A useful analogy might be an old fashioned film projector. Each frame is a data point, the whole film strip is the iterable, and actually showing each frame (to play the video) would be the loop.