# Python – Bitwise AND of List

Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among list elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using `reduce()` + lambda + “&” operator
The above functions can be combined to perform this task. We can employ reduce() to accumulate the result of AND logic specified by the lambda function. Works only with Python2.

 `# Python code to demonstrate working of ` `# Bitwise AND of List ` `# Using reduce() + lambda + "&" operator ` ` `  `# initializing list ` `test_list ``=` `[``4``, ``6``, ``2``, ``3``, ``8``, ``9``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Bitwise AND of List ` `# Using reduce() + lambda + "&" operator ` `res ``=` `reduce``(``lambda` `x, y: x & y, test_list) ` ` `  `# printing result  ` `print``(``"The Bitwise AND of list elements are : "` `+` `str``(res)) `

Output :

```The original list is : [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are : 0
```

Method #2 : Using` reduce()` + operator.iand
This task can also be performed using this method. In this the task performed by lambda function in above method is performed using `iand `function for cumulative AND operation. Works with Python2 only.

 `# Python code to demonstrate working of ` `# Bitwise AND of List ` `# Using reduce() + operator.iand ` `from` `operator ``import` `iand ` ` `  `# initializing list ` `test_list ``=` `[``4``, ``6``, ``2``, ``3``, ``8``, ``9``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Bitwise AND of List ` `# Using reduce() + operator.iand ` `res ``=` `reduce``(iand, test_list) ` ` `  `# printing result  ` `print``(``"The Bitwise AND of list elements are : "` `+` `str``(res)) `

Output :

```The original list is : [4, 6, 2, 3, 8, 9]
The Bitwise AND of list elements are : 0
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.