What does .dictreader() do?

i’m on that track right now, is iterators after files? is it before it? should i go reviewing?

It’s odd that the files unit would come before the iterators unit if it is using an iterator. It’s been so long so I did that course one couldn’t tell you what order it is arranged in. That suggests further that this lesson/unit needs to be segued to the other two units (exceptions and iterators), then come back to it.

what is it with iterators anyways? i know it’s computer iterating through a lit, range of numbers,even a string,etc, and doing something for or with every item in that list. what else is to it? and i know exceptions is a piece of code which tells computer so something else if this happened, or do not run some code if some condition is met

In Python, iterators are at the foundation of all/most iteration operations. Any object (that is a data structure) that belongs to a class that is iterable will inherit an __iter__() method from its parent class. Strings, lists, tuples, even sets are iterable and so have that method.

Create any one of them and assign it.

>>> a = set()


>>> dir(a)

In that long list of dunder methods, attributes and built in methods you will find,

, '__iter__',

It is that method that is invoked when we iterate over the list with for. Note that since iterables are consumable, it works from a copy of the list it is iterating. This is also the reason we cannot reverse course once iteration is underway. Interfering with iteration is another discussion, and not warranted for our purposes in most circumstances, so does not bear any consideration. Consuming an iterator is what we’re on about, here.

>>> a = range(10)
    # ... , '__iter__', ...

For people who tell you that range() is an iterator, you can tell them they are wrong and ask, ‘Why doesn’t it have a __next__ attribute?’ Well, that would be because it has an __iter__ attribute. We should note also the presence of a, __getitem__ attribute. That is worth some reading for a complete picture.

A range object is very nearly an iterator, but for the fact it is not one. We still need to iterate it to cast a list, tuple or set from it, but, it is never consumed. Once we create a range object, it exists until we assign something else to the variable it has been assigned to. This is the reason we write our range directly into a loop signature line… So that it does disappear, afterward. One would be derelict to omit bringing up generators, as there is some parallel here, however we don’t wish to deviate. Keep that on the back burner.

Iterator objects have a __next__ attribute, so if you ever dir() an object and find that, then you know you can iterate once only using next() in a while loop, as shown above. We would normally write in the exception handling but I left that out for simplicity sake. We can cover that once you’ve got up to speed with the stuff I’ve been suggesting you learn up on. Happy coding! Ping me when you are back to learning about the csv.DIctReader. I’ll be looking for you to teach me something!

1 Like

i guess i have a lot to check out…

1 Like

Lots to check out, yes, but nothing insurmountable given one spends the time becoming totally familiar with the inner workings of Python. For my own part, I have still only scratched the surface, confining most of my effort to the lower realm of the learning curve. That was my own choice. If a career is what you have in mind, then buckle up.

We can make our own iterators:

>>> a = iter(range(10))
>>> while True:
...     try:
...         print (next(a))
...     except StopIteration:
...         print ("Done")
...         break

You see now why Python gives us the for..in loop. It is simple and reliable, and won’t run on forever if we exclude the break.

Bottom line, once you get past all the syntax and basic usage, delve into the inner workings of Python to shore up that knowledge with real insight. Happy coding!

1 Like

i fully get the code you mentioned here, the only difference is that I’ve only learnt to iterate with for loop in this code.
i mean like this:

>>>for i in (range(10)):
>>>>>> while True:
...             print (i)
...             if i > 10:
...                 print ("Done")
...                 break

i guess i know what i need to learn now. ty!

1 Like

It will never be greater than 10, or even equal to 10. There is no 10 in range(10).

>>> print (list(range(10))
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

The idea of the for..in loop is manage its own logic:

for i in range(10):
    print (i)
print ('Done')

Careful not to complicate things for yourself. Connect the dots using the simplest code model, possible. Always keep it simple and slowly you will begin recognize useful patterns and constructs.

Woops, MB! that was for i += 1. you’re right!

1 Like