Fastest Python With Iterator Tools

Introduction

Iteration tools in Python are designed for programmers to efficiently loop through data and make it easier to work with data.

Python Itertools can also be used in machine learning projects. By speeding up Python, you can gain efficiency in your projects.

Increasing the efficiency of the program by reducing the cycles will provide a great efficiency to the programmer in many business areas and also provides memory control.

Testing Python Iterator Tools

We talked about speed difference and reducing memory usage. In this section, we will test them on Python.

# For calculate time
# For using operator
# For random number

import time
import operator as op
import numpy.random as np

x = np.randint(0 , 100 , 10000)
y = np.randint(0 , 100 , 10000)

# Starting timer
t1 = time.time()

# Subtract each element of lists
result = list(map(op.pow,x, y))

# Ending timer
t2 = time.time()

print("Time: %.6f" %(t2 - t1))
Time: 0.003602
t1 = time.time()

for i in range(1 , 10000):
  result = x[i] ** y[i]

t2 = time.time()

print("Time: %.6f" %(t2 - t1))
Time: 0.014477

The result is that not every loop does such simple operations, the time difference in longer loops will open up, now we know why iterator tools perform better.

Filter Function

The filter() method constructs an iterator from elements of an iterable for which a function returns true.

In simple words, filter() method filters the given iterable with the help of a function that tests each element in the iterable to be true or not.

It is a 2-parameter structure, the first parameter function takes the second parameter iterable value. There is no need to call the itertools library to use it.

nums = [1 , 2 , 3 , 4 , 5]

def FoundSpecial(num):
  special = [1 , 3 , 5]
  
  if (num in special):
    return True

  else:
    return False

filtered = filter(FoundSpecial, nums)

print("nums:" , end = " ")
for i in filtered:
  print(i , end = " ")
nums: 1 3 5

Here’s how the code works: the function returns true if it finds special numbers in the list. Each time the filter function returns true, it allocates and stores that element from the list.

The islice Function

Islice, a simple and very useful function, provides access to data in data types such as array, array, list, with start and end parameters.

The islice function is not ready like a filter, do not forget to import the itertools library. You can use the as the structure to shorten the name of itertools.

import itertools as it
str1 = "My Master Designer"

x = list(it.islice(str1, 0, 2))
print(x)
['M', 'y']

The first parameter gets the data types, the second parameter gets the start value and the last parameter gets the end value.

One of the biggest advantages of the Islice function is to be able to navigate through a list step by step. In short, you can reach the values in the range you specify (within the list) by skipping certain indexes.

import itertools as it

l1 = range(100)

# access from 0 to 50 fives by fives
x = list(it.islice(l1, 0, 50 ,5))
print(x)
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

As you can see, we have printed indexes from 0 to 50 divisible by 5 on the screen, as you can see in the example, you can set the start and end yourself, it isn’t defined according to the list.

Map Function

The map function is useful when operating between two lists. Recommended for use with the Operator module.

The operator module is a simple python module that translates the mathematical operations you use into functions. Below are some simple structures.

operator.pow = exponentiation
operator.sub = subtract
operator.add = addition
operator.mul = multiplication

You can use the functions here with the map function. The map function provides 2 main parameters, the first parameter is operation, the second parameter is data.

The map() function is included in Python’s built-in functions, so there’s no need to import anything.

import operator as op

l1 = [1 , 2  , 3]
l2 = [10 , 20 , 30]

# l1[0] + l2[0]...
print(list(map(op.add, l1 , l2)))
[11 , 22 , 33]

Instead of using the operator module, you can write your own functions and use them with the map function.

# Create list with 10 numbers
nums = [x for x in range(10)]

def func(number):
  return number + 111

print(list(map(func, nums)))
[111, 112, 113, 114, 115, 116, 117, 118, 119, 120]

The map function adds the data you entered in the parameter of the function, and runs the function and creates a list as a result.

index 0 + 111
index 1 + 111
index 2 + 111
index 3 + 111
...

Summary: Used to apply your own functions or pre-made functions to the elements in a list or tuple. You can use any function with map on list elements.

The Zip Function

The zip function, as the name suggests, is used to combine lists. However, the zip function works a little differently, you can find the working logic below.

x = ["Index","Index","Index"]
y = ["1st" , "2nd", "3rd"]

print(list(zip(y , x)))
[('1st', 'Index'), 
('2nd', 'Index'),
('3rd', 'Index')]

When combining two lists, it combines the index of the first list with the index of the second list. You can also combine more lists.

x = ["Index","Index","Index"]
y = ["First","Second","Third"]
z = ["X" , "Y" , "Z"]

print(list(zip(y , z , x)))
[('First', 'X', 'Index'),
('Second', 'Y', 'Index'),
('Third', 'Z', 'Index')]

Count Function

Let’s see more functions to improve performance, The count function is an iterator that will return values counting up by a particular step.

It is similar to the Range function, except for a few important differences, it gives better performance and you can determine how many times it will go.

The first parameter is used to set the start value, and the second parameter is used to set the amount of increment after the start value (like one by one, two by two).

import itertools as it

for i in it.count(0 , 2):
  if i == 10:
    break
  else:
    print(i , "x")
0 x
2 x
4 x
6 x
8 x

Important warning, remember to use the break construct, count does not have an ending value.

Cycle Function

It is similar to the Count function, with pros and cons. List, tuple, etc. of the entered data. accesses the structure’s data serially.

One notable portion of the use of this function as well is that it will create and save a copy of each iteration provided to it.

import itertools as its

l1 = [10 , 20 , 30 , 40 , 50]

for i in its.cycle(l1):
    print(i)

Don’t forget to set break, otherwise the loop will be an infinite loop.

Why Performance & Speed is Important?

Memory consumption, speed, such concepts are important for computer software, the biggest reason for this is customer satisfaction.

In games, mobile applications or computer applications, you should present the most fluent and fastest project to the user.

In areas such as data science and machine learning, performance and speed are even more important. It is a great success to reduce an algorithm that will continue for 1 hour to 30 minutes.

2 thoughts on “Fastest Python With Iterator Tools

  1. Why would you import islice and use it to slice a sequence then immediately turn that iterator into another sequence? The whole point of islice is that it can slice an iterator and return an iterator, to slice a string and turn it into a list just do [*’string'[0:2]]

    1. Thanks a lot for your feedback and comment, I realized I didn’t explain the islice function properly and I edited that header. I hope you liked the content.

      Have a nice day!

Leave a Reply

Your email address will not be published. Required fields are marked *