Open In App

Python List Inversions

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

List Inversions using map() + lambda + “~” operator

The above functions can be combined to perform this task. We can employ Python map() to accumulate the result of the Inversion logic specified by the Python lambda function.

Python3




# Python3 code to demonstrate working of
# List Inversions
# Using map() + lambda + "~" operator
 
# initializing list
test_list = [7, 8, 9, 1, 10, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# List Inversions
# Using map() + lambda + "~" operator
res = list(map(lambda x: ~x, test_list))
 
# printing result
print("The Bitwise Inversions of list elements are : " + str(res))


Output : 

The original list is : [7, 8, 9, 1, 10, 7]
The Bitwise Inversions of list elements are : [-8, -9, -10, -2, -11, -8]

Time Complexity: O(n) where n is the number of elements in the string list. The map() + lambda + “~” operator is used to perform the task and it takes O(n) time.

Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.

List Inversions using map() + operator.invert 

This task can also be performed using this method. In this, the task performed by the Python lambda function in the above method is performed using the operator for cumulative Inversion operation. 

Python3




# Python 3 code to demonstrate working of
# List Inversions
# Using map() + operator.invert
from operator import invert
 
# initializing list
test_list = [7, 8, 9, 1, 10, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# List Inversions
# Using map() + operator.invert
res = list(map(invert, test_list))
 
# printing result
print("The Bitwise Inversions of list elements are : " + str(res))


Output : 

The original list is : [7, 8, 9, 1, 10, 7]
The Bitwise Inversions of list elements are : [-8, -9, -10, -2, -11, -8]

List Inversions using for loop

  • First, define a list of integers my_list containing five elements. 
  • Define an empty list called inverted_list
  • Use a for loop to iterate over each element in my_list, invert it using the tilde operator, and append the result to inverted_list
  • Print the inverted_list to the console.

Python3




# give input list
my_list = [1, 2, 3, 4, 5
# take empty list
inverted_list = [] 
 
# iterate
for x in my_list:
  # using bitwise operator and append get the inverted list
    inverted_list.append(~x)
# print inverted list output
print(inverted_list)


Output

[-2, -3, -4, -5, -6]

Time complexity: O(N), where N is the length of the input list. This is because the for loop iterates over each element in the list once, and the operations inside the loop take constant time.
Auxiliary Space: O(N), we create a new list inverted_list to store the inverted elements. The size of this list is the same as the size of the input list.

List Inversions using numpy library

1. Define a list of integers `test_list`.
2. Print the original list.
3. Compute the bitwise NOT of each element in `test_list` using the `np.invert()` function from the `numpy` library.
   a. Convert `test_list` to a `numpy` array using the `np.array()` function.
   b. Call the `np.invert()` function on the array to compute the bitwise NOT of each element.
4. Convert the resulting `numpy` array to a Python list using the `tolist()` method.
5. Print the inverted list.
 

Python3




import numpy as np
 
test_list = [7, 8, 9, 1, 10, 7]
# printing original list
print("The original list is : " + str(test_list))
 
inverted_list = np.invert(test_list).tolist()
 
print("The Bitwise Inversions of \
        list elements are : " + str(inverted_list))


Output: 

The original list is : [7, 8, 9, 1, 10, 7]
The Bitwise Inversions of list elements are : [-8, -9, -10, -2, -11, -8]

Time complexity: The np.invert() function from the Numpy library is applied to the entire list in one call, which takes O(n) time. The tolist() method also takes O(n) time because it creates a new list with the same elements as the Numpy array. Therefore, the overall time complexity is O(n).

Auxiliary Space: The Numpy array created from the list takes O(n) space since it stores all the elements in memory. The tolist() method also creates a new list with the same number of elements, so it also takes O(n) space. Therefore, the overall space complexity is O(n).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads