List or Tuple Iteration in Python
List
or Tuple Iterations:
We
will travel on a entire list or tuple and extract each word. iteration takes
one word from a list or tuple. It is used with a loop also. हम पूरी सूची या टपल पर
यात्रा करेंगे और प्रत्येक शब्द को निकालेंगे। पुनरावृत्ति सूची या टपल से एक शब्द
लेता है। इसका उपयोग लूप के साथ भी किया जाता है।
First of all, calculate the length of given list or tuple.
Example: -
I
= [“Apple”, “Banana”,”Orange”]
T=len(i)
Print (t)
Output:
3
Iterate characters with length function
Example: -
I
= [“Apple”,”Banana”,”Orange”]
T=len(i)
Print (t)
For
a in range (t):
Print (i[a])
Output:
Apple
Banana
Orange
Iterate
reverse characters with length function
Example: -
i
= [“Apple”,”Banana”,”Orange”]
i=i
[-1::-1]
T=len(i)
Print (t)
For
a in range (t):
Print (i[a])
Output:
Orange
Banana
Apple
Example: -
i
= [“Apple”,”Banana”,”Orange”]
T=len(i)
Print (t)
For
a in range (t-1,-1,-1):
Print (i[a])
Output:
Orange
Banana
Apple
Iterate
characters without length function
Example: -
i
= “Hello Friends”
For
a in i:
Print (a)
Output:
Apple
Banana
Orange
Iterate
reverse characters without length function
Example: -
i
= [“Apple”,”Banana”,”Orange”]
i=i
[-1::-1]
For
a in i:
Print (a)
Output:
Orange
Banana
Apple
Iterators
An
iterator is an object that contains a countable number of values.
An
iterator is an object that can be iterated upon, meaning that you can traverse
through all the values.
Technically,
in Python, an iterator is an object which implements the iterator protocol,
which consist of the methods __iter__()
and __next__()
.
Iterator vs Iterable
Lists,
tuples, dictionaries, and sets are all iterable objects. They are
iterable containers which
you can get an iterator from.
All these
objects have a iter()
method which is used to get
an iterator:
Example
Return
an iterator from a tuple, and print each value:
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
Even strings are iterable objects, and can return an iterator:
Example
Strings
are also iterable objects, containing a sequence of characters:
mystr = "banana"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
Looping Through an Iterator
We can
also use a for
loop to iterate through an iterable object:
Example
Iterate
the values of a tuple:
mytuple = ("apple", "banana", "cherry")
for x in mytuple:
print(x)
Example
Iterate
the characters of a string:
mystr = "banana"
for x in mystr:
print(x)
The for
loop
actually creates an iterator object and executes the next() method for each
loop.
Create an Iterator
To create
an object/class as an iterator you have to implement the methods __iter__()
and __next__()
to
your object.
As you
have learned in the Python
Classes/Objects chapter, all classes have a function
called __init__()
, which allows you to do some
initializing when the object is being created.
The __iter__()
method
acts similar, you can do operations (initializing etc.), but must always return
the iterator object itself.
The __next__()
method
also allows you to do operations, and must return the next item in the
sequence.
Example
Create
an iterator that returns numbers, starting with 1, and each sequence will
increase by one (returning 1,2,3,4,5 etc.):
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
StopIteration
The
example above would continue forever if you had enough next() statements, or if
it was used in a for
loop.
To
prevent the iteration to go on forever, we can use the StopIteration
statement.
In
the __next__()
method, we can add a
terminating condition to raise an error if the iteration is done a specified
number of times:
Example
Stop after 20
iterations:
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
if self.a
<= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
Post a Comment