Python Itertools and Python Iterables
Python Iterables

Python Itertools and Python Iterables

Introduction to Python Iterables

An iterable in python is an object in python that can return an iterator. Using this python iterator, we can iterate on every single element of the iterable.

To check what happens internally in an iterator, we’re going to use the ‘dis’ module to disassemble the code. Once we import it, we call the dis() function. Before Preferring the example, let’s see python Syntax.

  1. >>> import dis
  2. >>> dis.dis('for _ in [1,2,3]:pass')
  3. 1 0 SETUP_LOOP 12 (to 14)
  4. 2 LOAD_CONST 4 ((1, 2, 3))
  5. 4 GET_ITER
  6. >> 6 FOR_ITER 4 (to 12)
  7. 8 STORE_NAME 0 (_)
  8. 10 JUMP_ABSOLUTE 6
  9. >> 12 POP_BLOCK
  10. >> 14 LOAD_CONST 3 (None)
  11. 16 RETURN_VALUE

Here, GET_ITER is like invoking iter(). Likewise, FOR_ITER is to repeatedly call next() to get each element.

3. Example of Python Iterables

Okay, so let’s take an example before we begin.

  1. >>> nums=[1,2,3]
  2. >>> number=iter(nums)
  3. >>> next(number)

1

  1. >>> next(number)

2

  1. >>> next(number)

3

  1. >>> next(number)
  2. Traceback (most recent call last):
  3. File "<pyshell#224>", line 1, in <module>
  4. next(number)

StopIteration

In this code, ‘nums’ is a python iterables. Using the function iter(), we create an iterator ‘number’. This relationship can be inferred from the following representation.

An Example of Java Iterables

An iterables in python has the methods __len__() and __getitem__()

  1. >>> a=[1,2,3]
  2. >>> dir(a)

[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]

  1. >>> a.__len__()

3

  1. >>> a.__getitem__(2)

3

While __len__() returns the length of the python iterables, __getitem__() takes an index as an argument, and returns the value at that position in the iterable in python.

Why should you learn Python? 

4. More Iterables in Python

Most containers are python iterables. Let’s see some relationship using the built-in function issubclass().

  1. >>> import collections
  2. >>> issubclass(collections.Iterator,collections.Iterable)

True

#This means an iterator is a python iterable.

  1. >>> issubclass(collections.Iterable,collections.Iterator)

False

  1. >>> issubclass(collections.Generator,collections.Iterator)

True

#A generator is a Python iterator

  1. >>> issubclass(collections.Generator,collections.Iterable)

True

#So, a generator is also an iterable in python

Let’s check for some more constructs.

  1. >>> issubclass(collections.Set,collections.Iterable)

True

#A set is a python iterable

  1. >>> issubclass(collections.UserDict,collections.Iterable)

True

  1. >>> issubclass(collections.deque,collections.Iterable)

True

  1. >>> issubclass(collections.deque,collections.Iterable)

True

  1. >>> issubclass(collections.defaultdict,collections.Iterable)

True

  1. >>> issubclass(collections.OrderedDict,collections.Iterable)

True

When we say that an iterator is a python iterable, we mean it. In the following code, we define an iterator, and then call the iter() function on it.

  1. >>> a=iter([1,2,3])
  2. >>> b=iter(a)
  3. >>> b
  4. <list_iterator object at 0x0328BCB0>
  5. >>> a
  6. <list_iterator object at 0x0328BCB0>
  7. >>> next(b)

1

  1. >>> next(a)

2

  1. >>> next(b)

3

  1. >>> next(a)
  2. Traceback (most recent call last):
  3. File "<pyshell#242>", line 1, in <module>
  4. next(a)

StopIteration

What we get back is a python iterator. Hence, an iterator is python iterable. In this code, hence, b and a reference to the same iterator. Thus, when we call next() on either one, the state is changed for both.

Top books to learn Python Programming Language.

Read Complete Article>>

See Also-


要查看或添加评论,请登录

社区洞察

其他会员也浏览了