Python – Bitwise OR among list elements


Introduction

Python Language comes under the OOPS concept and it runs the code immediately without checking for errors. This language was discovered by Guido Rossum in 1989 and was globally launched in the year 1991. Python is a versatile and high-level language that can be understood easily by the user. In the current world, handling data is the most challenging task for organizations with a high volume of data and with the development of data science and machine learning it has become easier to access.

Bitwise OR among list elements

The Operator used for representing the bitwise operator is “|â€. Bitwise OR can also be defined as a set union using the function union(). The example of a bitwise OR operator is given with two variables,

A = [2, 4, 6, 9]
B = [3, 6, 7]

The above elements can undergo an bitwise OR operation in two ways namely,

Case 1

result= A|B

Case 2

result = B.union(A)

Approach

Approach 1 − Using the iteration method

Approach 2 − Using the lambda method

Approach 3 − Using numpy module

Approach 1: Python – Bitwise OR among list elements using iteration method

Algorithm

  • Step 1 − Initialize the list data structure with three integer elements 40, 50 and 90.

  • Step 2 − Then assigning the out variable to “0boo†which equals to 0 in decimal

  • Step 3 − Each element of the list performs the bitwise OR operation.

  • Step 4 − The current value of out and the current element of list_1 are compared and stored in the variable named “outâ€.

  • Step 5 − Then the print statement will return the value after performing the bitwise OR operator.

Example

#initializing the list with three integer elements
list_1=[40,50,90]
#the output is assigned a "0" decimal value
out=0b00
#for loop is used to iterate through the list using range() and length of the list
for a in range(len(list_1)):
   #list returns bitwise OR operator of the list
   out|=list_1[a]

#it returns the value after performing bitwise OR operator
print("List after performing the operation",out)

Output

List after performing the operation 122

Approach 2: Python – Bitwise OR among list elements using lambda function

Algorithm

  • Step 1 − The required module is imported to use the reduce() function.

  • Step 2 − Define the list data structure with three integer elements 40, 50 and 90.

  • Step 3 − The lambda function is used to perform the bitwise OR operation using the symbol “|â€.

  • Step 4 − For this the key parameter is used and then the lambda function uses the reduce function to get the result.

  • Step 5 − Then the print statement will return the value after performing the bitwise OR operator in two forms namely in binary format and decimal format.

Example

#importing the functools module to use the reduce function
from functools import reduce

#initializing the list with three integer elements
list_1 = [40, 50, 90]

#lambda function is used to find the bitwise OR with the key parameter
#after generating the result, it is reduced using the reduce() function and stored in out variable
result = reduce(lambda a, b: a | b, list_1)
#it returns the value after performing bitwise OR operator
print("The Bitwise OR operator of the given list in binary:",bin(result))

print("The Bitwise OR operator of the given list :",result)

Output

The Bitwise OR operator of the given list in binary: 0b1111010
The Bitwise OR operator of the given list : 122

Approach 3: Python – Bitwise OR among list elements using numpy module

Algorithm

  • Step 1 − The required module is imported to use the “np†function.

  • Step 2 − Initialize the list data structure with three integer elements 40, 50, and 90.

  • Step 3 − The function named “np.bitwise_or.reduce()†is used to perform the bitwise OR operation for all the elements of the list.

  • Step 4 − Then the print statement will return the value after performing the bitwise OR operator in decimal format.

#importing the numpy module to use the "np" function
import numpy as np

#initializing the list with three integer elements
list_1 = [40, 50, 90]

#direct function to add the elements of the list is done
result = np.bitwise_or.reduce(list_1)

#it returns the value after performing bitwise OR operator
print("List after performing the operation", result)

Output

List after performing the operation 122

Conclusion

In this article, the three approaches are depicted to perform the bitwise OR operation. In the first approach, the for loop is used to iterate through the elements of the list using the range() and len() methods. In the second approach, the lambda functions are used along with the reduce() functions. When the functions are not defined or it is unknown, the lambda function is used efficiently. In the last approach, the numpy module is imported to use the “np†function, to perform the bitwise OR operation.

Updated on: 25-Aug-2023

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements