Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
Iterable Objects of Built-in Data Types
This section lists all built-in data types that can be used to create iterable objects.
As of Python 3.10, objects created from many built-in data types are iterable objects.
Here is an example Python code showing you iterable built-in data types. It uses the "collections" module to help testing whether an object is iterable or not.
herong$ more built-in-iterable.py
# built-in-iterable.py
#- Copyright 2022 (c) HerongYang.com. All Rights Reserved.
#
from collections.abc import Iterable
o = bytes(b'\x41\x42\x43\x44')
print('bytes is iterable:', isinstance(o, Iterable))
o = bytearray(b'\x41\x42\x43\x44')
print('bytearray is iterable:', isinstance(o, Iterable))
o = memoryview(b'\x41\x42\x43\x44')
print('memoryview is iterable:', isinstance(o, Iterable))
o = str('ABCD')
print('str is iterable:', isinstance(o, Iterable))
o = tuple(['apple', 'orange'])
print('tuple is iterable:', isinstance(o, Iterable))
o = list(('apple', 'orange'))
print('list is iterable:', isinstance(o, Iterable))
o = dict(foo=100, bar=200)
print('dict is iterable:', isinstance(o, Iterable))
o = set(['apple', 'orange'])
print('set is iterable:', isinstance(o, Iterable))
o = frozenset(['apple', 'orange'])
print('frozenset is iterable:', isinstance(o, Iterable))
herong$ python built-in-iterable.py
bytes is iterable: True
bytearray is iterable: True
memoryview is iterable: True
str is iterable: True
tuple is iterable: True
list is iterable: True
dict is iterable: True
set is iterable: True
frozenset is iterable: True
"tuple" is a commonly used iterable data type. So let's see how it supports the iterable interface.
>>> t = tuple(range(5))
>>> i = iter(t)
>>> print("object t: id and type =", id(t), type(t))
object t: id and type = 4402847552 <class 'tuple'>
>>> print("object i: id and type =", id(i), type(i))
object i: id and type = 4402522912 <class 'tuple_iterator'>
>>> n = 0
>>> for x in t:
... n += 1
... print(x)
... if n == 3: break
...
0
1
2
>>> n = 0
>>> for x in t:
... n += 1
... print(x)
... if n == 3: break
...
0
1
2
Above output tells me that:
As an iterable object, "tuple" also has a less known behavior. Its iterator object is also an iterable object, see the example code below.
>>> t = tuple(range(5)
>>> i = iter(t)
>>> j = iter(i)
>>> print("object i: id and type =", id(i), type(i))
object i: id and type = 4402522912 <class 'tuple_iterator'>
>>> print("object j: id and type =", id(j), type(j))
object j: id and type = 4402522912 <class 'tuple_iterator'>
>>> i = iter(t)
>>> n = 0
>>> for x in i:
... n += 1
... print(x)
... if n == 3: break
...
0
1
2
>>> n = 0
>>> for x in i:
... n += 1
... print(x)
... if n == 3: break
...
3
4
Above output tells me that:
By the way, Python standard actually requires that all "iterator" objects to implement the __iter__() method and return themselves. In other words, all "iterator" objects are special "iterable" objects. They can be used to iterate all items only in one round.
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
►Iterable Objects of Built-in Data Types
What Is Filtered Generator Expression
What Is Double-Generator Expression
List, Set and Dictionary Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager