By Sri Venkata Sai Kiran Chaitanya Agnihotram
In this blog, we'll explore some of the lesser-known functions from various libraries that can generate iterable objects, and discuss how to use them effectively in our code.
Functions that return iterable objects as output are incredibly useful in Python programming.
In this blog, we'll explore some of the lesser-known functions from various libraries that can generate iterable objects, and discuss how to use them effectively in our code.From itertools to more obscure libraries, you'll discover a wealth of tools that can help you streamline your code and make your programming more efficient.
Let’s get started by knowing about “Iterable Objects”
Iterables are a fundamental part of many Python libraries and frameworks, and are used extensively in data processing, machine learning, web development, and other areas of programming.
In Python, an iterable object is any object that can be looped over using a for loop.It is an object that implements the iterable protocol, which requires that the object has a method called __iter__() that returns an iterator object.The iterator object, in turn, must have a method called __next__() that returns the next item in the sequence, and raises a StopIteration exception when there are no more items.They make it possible to write code that can operate on any type of data, as long as it can be represented as a sequence of items.This includes simple data types like lists, tuples, and strings, and more complex objects like files, databases, and network connections.
Now that we have understood what iterable objects are, let’s dive into some Python functions that generate iterable objects and how to use them effectively.In Python, many functions can generate iterable objects as output. Some of these functions are built-in functions in Python, while others are part of different libraries.
Let’s discuss various Built-in functions in Python that provide an efficient way to work with iterable objects.
my_bytes = bytearray([97, 98, 99]) for byte in my_bytes: print(byte)
Output:
97
98
99
numbers= [1, 2, 3] letters = ['a', 'b', 'c'] zipped_list = zip(numbers, letters) for pair in zipped_list: print(pair)
Output:
(1, 'a')
(2, 'b')
(3, 'c')
my_list = ['a', 'b', 'c'] for i, value in enumerate(my_list): print(i, value)
Output:
0 a
1 b
2 c
Let's now move on to explore functions that generate iterable objects from other libraries in Python.
The most commonly used library for iterable objects is the Itertools module, which contains several functions that return iterable objects such as permutations, combinations, and product.
from itertools import compress data = [1, 2, 3, 4, 5] selectors = [True, False, True, False, True] result = compress(data, selectors) print(list(result))
Output:
[1, 3, 5]
- accumulate(iterable, func=None): This function returns an iterator that produces the accumulated results of applying the func function to the elements of the iterable. If func is not specified, the default function is the addition.
(Default Use):
from itertools import accumulate data = [1, 2, 3, 4, 5] result = accumulate(data) print(list(result))Output:[1, 3, 6, 10, 15]
(Can be used to calculate factorial):
from itertools import accumulate numbers = [2, 3, 4, 5] result = accumulate(numbers, func=lambda x, y: x*y) print(list(result))Output:
[2, 6, 24, 120]
from itertools import groupby data = [("A", 1), ("A", 2), ("B", 3), ("B", 4), ("B", 5)] # grouping the data based on first element of each tuple groups = groupby(data, lambda x: x[0]) # iterating through the groups and printing the keys and items for key, group in groups: print(key) for item in group: print(item)
Output:
A
('A', 1)
('A', 2)
B
('B', 3)
('B', 4)
('B', 5)
- permutations(iterable, r=None) : This function returns an iterator that produces all possible permutations of the elements in the iterable. The optional parameter r specifies the length of the permutations (defaults to the length of the iterable).
(With ‘r’=2 )
from itertools import permutations data = ['A', 'B', 'C'] result = permutations(data, 2) print(list(result))Output:
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
(For all Permutations of ‘ABC”):
from itertools import permutations def get_permutations(s: str): arr = [] for r in range(1, len(s) + 1): for i in permutations(s, r): arr.append(''.join(i)) return arr print(get_permutations('ABC'))Output:
['A', 'B', 'C', 'AB', 'AC', 'BA', 'BC', 'CA', 'CB', 'ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
Numpy offers several functions to generate iterable objects for different mathematical operations, making it easier to work with large datasets and perform computations efficiently.
import numpy as np arr = np.array([[1, 2], [3, 4]]) for index, value in np.ndenumerate(arr): print(f"Index: {index}, Value: {value}")
Output:
Index: (0, 0), Value: 1
Index: (0, 1), Value: 2
Index: (1, 0), Value: 3
Index: (1, 1), Value: 4
import numpy as np # create a 2D numpy array arr = np.array([[1, 2], [3, 4]]) # iterate over all the indices of the array for index in np.ndindex(arr.shape): print(index, arr[index])
Output:
(0, 0) 1
(0, 1) 2
(1, 0) 3
(1, 1) 4
import numpy as np it = iter(range(5)) arr = np.fromiter(it, dtype=int) print(arr)
Ouput:
[0 1 2 3 4]
Besides the functions we already discussed, Pandas also has several other functions like iterrows(), itertuples(), groupby(), etc. that generate iterable objects as outputs.
import pandas as pd df = pd.DataFrame({'Name': ['John', 'Sara', 'David'],'Age': [30, 25, 40],'Country': ['USA', 'Canada', 'UK']}) for index, row in df.iterrows(): print(index, row['Name'], row['Age'], row['Country'])
Output:
0 John 30 USA
1 Sara 25 Canada
2 David 40 UK
import pandas as pd df = pd.DataFrame({'Name': ['John', 'Sara', 'David'],'Age': [30, 25, 40],'Country': ['USA', 'Canada', 'UK']}) for row in df.itertuples(): print(row.Name, row.Age, row.Country)
Output:
John 30 USA
Sara 25 Canada
David 40 UK
import pandas as pd df = pd.DataFrame({'Name': ['John', 'Sara', 'David', 'Mary', 'Steve'], 'Age': [30, 25, 40, 28, 35], 'Country': ['USA', 'Canada', 'UK', 'USA', 'UK']}) groups = df.groupby('Country') for name, group in groups: print(name) print(group)
Output:
Canada
Name Age Country
1 Sara 25 Canada
UK
Name Age Country
2 David 40 UK
4 Steve 35 UK
USA
Name Age Country
0 John 30 USA
3 Mary 28 USA
Scipy is a popular library used for scientific computing and technical computing in Python. It provides several functions that generate iterable objects as outputs, making it easier to perform complex calculations and analysis.
from scipy.special import comb arr = [1, 2, 3, 4] combos = comb(arr, 2) print(list(combos))
Output:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
from scipy.special import perm arr = [1, 2, 3] perms = perm(arr) print(list(perms))
Output:
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
from scipy.spatial import distance_matrix # Create an array of points in 2D space points = [[0, 0], [1, 1], [2, 2]] # Calculate the pairwise Euclidean distances between the points distances = distance_matrix(points, points) # Print the resulting distances array print(distances)
Output:
[[0. 1.41421356 2.82842712]
[1.41421356 0. 1.41421356]
[2.82842712 1.41421356 0. ]]
The collections module in Python provides alternative implementations of several built-in types with additional features. It also provides some useful data structures such as deque, defaultdict, OrderedDict, and Counter.
Here are some examples of iterable generating functions from the collections module:
from collections import deque my_deque = deque([1, 2, 3, 4]) for item in my_deque: print(item)
Output:
1
2
3
4
from collections import defaultdict my_dict = defaultdict(int) my_dict["a"] = 1 my_dict["b"] = 2 for key, value in my_dict.items(): print(key, value)
Output:
a 1
b 2
from collections import OrderedDict my_dict = OrderedDict() my_dict["c"] = 3 my_dict["b"] = 2 my_dict["a"] = 1 for key, value in my_dict.items(): print(key, value)
Output:
c 3
b 2
a 1
from collections import Counter my_list = [1, 2, 3, 2, 1, 2, 3, 1] my_counter = Counter(my_list) for key, value in my_counter.items(): print(key, value)
Output:
1 3
2 3
3 2
Submitted by Sri Venkata Sai Kiran Chaitanya Agnihotram (SaiKiran0267)
Download packets of source code on Coders Packet
Comments