Python | Elementwise AND in Tuples

In this program, we are given two tuples. We need to create a Python program to perform element wise AND operation in tuple elements.
Submitted by Shivang Yadav, on November 13, 2021

Tuples in Python are common types of collections that are used commonly for storing data records in python for processing. In fields like data science and AI, we need to process data tuples to extract fruitful information. In this program, we need to perform element-wise AND operation in the tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Elementwise AND operation in Tuple

We will seek each element of both tuples and perform AND operations on the same index. And return all the resultant values of the AND operation.

Input:
tup1 = (3, 1, 4), tup2 = (5, 2, 6)

Output:
(1, 0, 4)

Methods for performing elementwise AND operation in the tuple,

Method 1:

One method to perform AND operation on each element of the tuple is by using generator expression for performing the AND operation and zipping together both the tuples using the zip() method.

# Program to perform elementwise AND operation 
# on Tuples using generator expression

# initializing and printing tuples 
tup1 = (3, 1, 4) 
tup2 = (5, 2, 6) 
print("All elements of Tuple 1 are " + str(tup1)) 
print("All elements of Tuple 2 are " + str(tup2)) 
  
# Performing AND operation on each element of the tuple,
andTup = tuple(val1 & val2 for val1, val2 in zip(tup1, tup2)) 
  
# printing result 
print("The tuple formed after elementwise AND operation are " + str(andTup)) 

Output:

All elements of Tuple 1 are (3, 1, 4)
All elements of Tuple 2 are (5, 2, 6)
The tuple formed after elementwise AND operation are (1, 0, 4)

Method 2:

Another approach that can solve the problem is by using "iand" to perform an extended logic of the AND operation and map it using map() and then convert the map to a tuple using the tuple() method.

# Program to perform elementwise AND operation 
# on Tuples using generator expression

# Importing iand operator 
from operator import iand 

# initializing and printing tuples 
tup1 = (3, 1, 4) 
tup2 = (5, 2, 6) 
print("All elements of Tuple 1 are " + str(tup1)) 
print("All elements of Tuple 2 are " + str(tup2)) 
  
# Performing AND operation on each element of the tuple,
andTup = tuple(map(iand, tup1, tup2))  
  
# printing result 
print("The tuple formed after elementwise AND operation are " + str(andTup)) 

Output:

All elements of Tuple 1 are (3, 1, 4)
All elements of Tuple 2 are (5, 2, 6)
The tuple formed after elementwise AND operation are (1, 0, 4)

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.