Python Iterator vs Iterable
 

Python Iterator vs Iterable

 

In Python, you might come up with the terms like iterator, iterable, iteration, etc. This can be confusing for many of you especially for those who are beginners.

So in this article, you will see the differences among all of them.

Iterable

The iterable in Python is anything you can loop over with a for loop. This includes the sequence types such as list, tuple, string, etc, and other than sequence type which includes set, dictionaries, file objects, and sockets.

An object is said to be iterable if you can get an iterator of it –

  • It generates an iterator when passed to iter() method
  • You can not directly use the next() method to manually iterate over iterable you need to convert it to iterator using the iter() method. If you call the next() method on an iterator you will get the next item

For example:

lst1= [1,2,3,'Hi']
for i in lst1:
    print(i)

In this example, lst1 is iterable when for loop start first it will call the _iter_() method.

Iterator

An iterator is an object that represents a stream of data. It implements the iterator protocol that consists of two methods i.e. _iter_() and _next_(). You can change an iterable to an iterator object by using the iter() method.

You can manually loop over an iterator object by passing it to next() method. If there are no more elements in the stream, calling the next() method will raise the StopIteration exception

Example 1:

The following example shows how you can change an iterable to iterator object using the iter() method.

lst1=[1,2,3,'Hi']
tpl1=(1,2,3,4)
str1='This code is in Python'
print (iter(lst1))
print (iter(tpl1))
print (iter(str1))

This will print the iterator objects and their memory location.

Example 2:

This example shows how you can manually iterate over elements of an iterator using the next() method.

lst1=[1,2,3,'Hi']
iterator=iter(lst1)
print(next(iterator))

This will print the first item of the given list. To print the next item you need to call the next() method again. Now you can see the output of this in the given image –

Iteration

Iteration is the repetition of a process in order to generate an outcome. In the following example, we will call it the first iteration when for loop executes and prints the first element of the list.

lst1= [1,2,3,'Hi']
for i in lst1:
    print(i)

In each iteration, the elements of lst1 will get printed.

Conclusion

Ok, that’s it for now. I hope now you know what iterable, iterator and iteration means. If you have any query then leave it in the comments below.

Previous Python Set Comprehension
Next Python enumerate() function
 

About author

You might also like

Python Programming 0 Comments

Variables and Constants In Python

A variable in Python is used to store a value in the memory. The value of a variable can be changed later in the program. In Python programming, a variable

Python Programming 0 Comments

Using Python pass keyword

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code. As empty code is not allowed in

Python Programming 0 Comments

Python List Comprehension

A python list can hold multiple different types of values in a single variable. In our previous article on Python list data type, we had discussed the Python lists, how

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply