Hello my friends in this Entry we are going to talk about a really cool topic (Well that's what I think) that is Iterators and Generators with the Python Programming Language, and for explaining each of this I'm going to build a class representing a Linked List data structure and how you can use Python Built-in Functions to iterate, create iterators or generators on custom classes.

#
Linked List

Accordinly to the Wikipedia, “a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence”.

And here I give you a class implementation of the Linked List already built with some basics helper methods, most of this code I took it from the blog article of Adnan Alam, he explains really well this class, though I made some changes from the original code.

## linkedlist.py

` `

classNode:

` `

def__init__(self,data=None,next_node=None):

self.data = data

self.next_node = next_node

` `

defget_data(self):

returnself.data

` `

defset_data(self, data):

self.data = data

` `

defget_next(self):

returnself.next_node

` `

defset_next(self,new_node):

self.next_node = new_node

` `

def__str__(self):

` `

returnself.data.__str__()

` `

classLinkedList:

` `

def__init__(self, front=None):

self.front = front

` `

definsert_front(self, data):

new_node = Node(data)

new_node.set_next(self.front)

self.front = new_node

` `

definsert_rear(self, data):

` `

new_node = Node(data)

ifself.frontisNone:

self.front = new_node

else:

current = self.front

whilecurrent.get_next():

current = current.get_next()

` current.set_next(new_node)`

` `

defdelete(self, data):

` `

current = self.front

prev = None

whilecurrent:

ifcurrent.get_data() == data:

ifprev:

` prev.set_next(current.get_next())`

else:

self.front = current.get_next()

break

else:

prev = current

current = current.get_next()

` `

defsearch(self, data):

current = self.front

whilecurrent:

ifcurrent.get_data() == data:

returnTrue

else:

current = current.get_next()

returnFalse

` `

def__len__(self):

current = self.front

count = 0

whilecurrent:

count +=1

current = current.get_next()

returncount

` `

def__str__(self):

current = self.front

temp = []

whilecurrent:

` temp.append(current.get_data())`

current = current.get_next()

` `

returntemp.__str__()

Here we have two classes, a class for

**Node**and a class for**LinkedList**, if you are not used to Object Oriented Programming in Python take a look at my two earlier entries in this topic.
Now as mentioned it before, this data structure could represent a sequence, so this could mean that we can index or iterate through it, well not yet, we need to provide to this class the methods that will allow us to perform these operations.

#
Set and Get Item

In our own data structures sometimes we want to have the capability to perform indexing access to the sequences, in same way we get and set elements in a Python List, we can achieved this by implementing __getitem__ and __setitem__ methods to our class, Python List are really versatile in this kind of operations, you can even slice using square brackets, but we are going to go that far, we are going to implement simple index access capable of doing even negatives, some validations must be done like only integers and values less or equal than the size of the sequence, let's build these methods.

def__getitem__(self, key):

` `

iftype(key) != int:

` `

raiseTypeError

` `

size = self.__len__()

` `

ifkey < 0andabs(key) > size:

` `

raiseIndexError

` `

ifkey < 0:

` `

key = size + key

` `

ifkey > size - 1:

` `

raiseIndexError

` `

current = self.front

counter = 0

whilecurrent:

ifcounter == key:

returncurrent

else:

current = current.get_next()

counter += 1

` `

def__setitem__(self, key, data):

` `

iftype(key) != int:

` `

raiseTypeError

` `

size = self.__len__()

` `

ifkey < 0andabs(key) > size:

` `

raiseIndexError

` `

ifkey < 0:

` `

key = size + key

` `

ifkey > size - 1:

` `

raiseIndexError

` `

current = self.front

counter = 0

whilecurrent:

ifcounter == key:

current.set_data = data

else:

current = current.get_next()

counter += 1

Now let's make some tests on this

>>> item1 = 2020

>>> item2 = 4040

>>> item3 = 6060

>>> item4 = 8080

>>> linked = LinkedList()

>>> linked.insert_front(item1)

>>> linked.insert_front(item2)

>>> linked.insert_front(item3)

>>> linked.insert_front(item4)

>>>

[8080, 6060, 4040, 2020]

>>> linked[1]

6060

>>> linked[2]

4040

>>> linked[-1]

2020

>>> linked[2] = 3030

>>>

[8080, 6060, 3030, 2020]

Pretty cool right? Now If we want to iterate through this linked list using a for loop, is not possible yet and that is because, so far this class is not an

**iterator**.#
Iterators

we turn these objects into iterators if we build the

**__iter__**and**next**methods to our**LinkedList**class, these methods are used by the for loop for iteration, and the code is as follow.def__iter__(self):

self.n = 0

returnself

` `

defnext(self):

ifself.n < self.__len__():

result = self.__getitem__(self.n)

self.n += 1

returnresult

else:

raiseStopIteration

As you can see if there is no more elements to iterate we need to raise a stop iteration error, this will indicate the for loop to stop, let's try this now.

>>> item1 = 101

>>> item2 = 102

>>> item3 = 103

>>> item4 = 104

>>> linked = LinkedList()

>>> linked.insert_front(item1)

>>> linked.insert_front(item2)

>>> linked.insert_front(item3)

>>> linked.insert_front(item4)

>>>

[104, 103, 102, 101]

>>>foriinlinked:

` `

` `

104

103

102

101

Something to keep in mind is that for Python 3, next for classes is used with double underscore

**__next__**; now that we have cover iterators let's talk about generators.#
Generators

Generators are a kind of iterators, well they behave like iterators, but they are implemented as functions and instead of returning a value they yield a value, the main advantage of this is that they do not allocate all the values in memory rather calculated on the fly, that is why is called generators, you won't find too much differences in terms of performance until you find you self in need of processing really large data sets.

A really simple generator is the calculation of Fibonacci numbers; here I show you a simple code for this.

deffibonacci(n):

"""

Yields the first n elements of the Fibonnaci

"""

a = b = 1

foriinrange(n):

yielda

a, b = b, a + b

Now let's test this

>>> gen1 = fibonacci(4)

>>> gen1.next()

1

>>> gen1.next()

1

>>> gen1.next()

2

>>> gen1.next()

3

>>> gen1.next()

` `

Traceback (most recent call last):

File "<pyshell#13>", line 1,in<module>

` gen1.next()`

StopIteration

>>>foriinfibonacci(10):

` `

` `

1

1

2

3

5

8

13

21

34

55

Something to point out here is that generators as well iterators can only be iterated with next once until they reach the end, and they can be easily used with for loops, now let's say we want to convert our Linked List into a generator; we can build a generator method and yield each value at a time.

defgen(self, n=None):

` `

ifn == None:

` `

n = self.__len__()

` `

foriinrange(n):

` `

yieldself.__getitem__(i)

And finally let's test this

>>> item1 = 2020

>>> item2 = 4040

>>> item3 = 6060

>>> item4 = 8080

>>> linked = LinkedList()

>>> linked.insert_front(item1)

>>> linked.insert_front(item2)

>>> linked.insert_front(item3)

>>> linked.insert_front(item4)

>>>

[8080, 6060, 4040, 2020]

foriinlinked.gen():

` `

` `

8080

6060

4040

2020

Well remember that this has meaning if there are too much elements in the LinkedList, so technics to process this information is required in order to improve execution performance.

We have come to the end of this entry; well I hope you have found this entry helpful to you. So thanks for coming by my new entry and I hope you have enjoyed this as much as I enjoyed writing it, stop by the comments if you want to discuss about this, share in your social media and subscribe. Cheers my friends.