Python | Binary list to integer
The problem being discussed in this article is quite common and every programmer would have come across it. Conversion of binary number list to its integer value can be done using shorthands and knowledge of them can prove to be quite useful. Let’s discuss certain ways in which this can be done.
Method #1: Using join() + list comprehension
The combination of these two function can help to achieve this particular task. In this method, the entire list is first converted to string and then type conversion into int and then binary number is obtained.
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
res = int ("".join( str (x) for x in test_list), 2 )
print ( "The converted integer value is : " + str (res))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using bit shift + | operator This particular task can be performed by shifting the bits and taking the | with each of the bits being processed. This is yet another elegant way in which this can be performed.
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
res = 0
for ele in test_list:
res = (res << 1 ) | ele
print ( "The converted integer value is : " + str (res))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time complexity: O(n), where n is the length of the binary list test_list.
Auxiliary Space: O(1), constant extra space is used for the integer res to store the converted result.
Method #3: Using for loop, without any built in methods
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
res = 0
j = 0
for i in range ( len (test_list), 0 , - 1 ):
x = 2 * * j
res + = x * test_list[i - 1 ]
if (j > len (test_list)):
break
j + = 1
print ( "The converted integer value is : " + str (res))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary space complexity: O(1), since the required extra space is constant, only integer variables “res” and “j”.
Method #4: Using reduce()
Another approach you can use to convert a binary list to an integer is to use the reduce function from the functools module. The reduce function applies a function to the elements of a list, cumulatively reducing the list to a single value.
For example, you can define a function that takes two elements of the list and combines them by multiplying the first element by 2 and adding the second element, which effectively performs the conversion of a binary list to an integer. You can then use the reduce function to apply this function to the elements of the list and get the final result:
Python3
from functools import reduce
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
def combine(x, y):
return 2 * x + y
res = reduce (combine, test_list)
print ( "The converted integer value is : " + str (res))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time complexity: O(n), where n is the length of the list, since the reduce function processes each element of the list once.
Auxiliary space: O(1), since the reduce function does not create any additional data structures.
Method #5 : Using math.pow() and for loop
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
res = 0
j = 0
for i in range ( len (test_list), 0 , - 1 ):
import math
x = math. pow ( 2 , j)
res + = x * test_list[i - 1 ]
if (j > len (test_list)):
break
j + = 1
print ( "The converted integer value is : " + str ( int (res)))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time Complexity : O(n)
Auxiliary Space : O(n)
Method #6: Using the built-in function int() and join()
This method is a simple and efficient way to convert a binary list to an integer by using the int() function and join() method.
Steps:
- Convert the binary list to a string using join() method.
- Use int() function to convert the binary string to an integer.
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
print ( "The original list is : " + str (test_list))
binary_string = ''.join( map ( str , test_list))
res = int (binary_string, 2 )
print ( "The converted integer value is : " + str (res))
|
Output
The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38
Time complexity: O(n), where n is the length of the binary list.
Auxiliary space: O(n), where n is the length of the binary list.
Method#7 Using the sum() function with a generator expression
step-by-step algorithm for the approach in the code:
- Initialize the binary list test_list.
- Reverse the test_list using the reversed() function and iterate over its elements along with their index using the enumerate() function.
- For each element in the reversed test_list, multiply it with 2 raised to the power of its index.
- Sum up all the products obtained in step 3 to get the decimal equivalent of the binary number.
- Print the decimal equivalent obtained in step 4.
Python3
test_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
res = sum (x * 2 * * i for i, x in enumerate ( reversed (test_list)))
print (res)
|
Time complexity:
The time complexity of this approach is O(n), where n is the length of the binary list test_list. This is because the approach involves iterating over the reversed test_list only once to calculate the decimal equivalent of the binary number.
Auxiliary space complexity:
The auxiliary space complexity of this approach is O(1), as it does not require any additional space to store variables or data structures.
Method#8:Using the struct module and the pack() function
step-by-step algorithm for implementing the approach using the struct module and the pack() function to convert a binary list to an integer:
- Convert the binary list to a binary string by joining the elements of the list using the join() function and the str() function applied to each element.
- Convert the binary string to an integer with a base of 2 using the int() function.
- Pack the resulting integer into a binary string using the pack() function with a format string of ‘>I’, which specifies a big-endian unsigned integer.
- Unpack the resulting binary string using the unpack() function with a format string of ‘>i’, which specifies a big-endian signed integer.
- Extract the integer value from the resulting tuple and store it in a variable.
Python3
import struct
binary_list = [ 1 , 0 , 0 , 1 , 1 , 0 ]
binary_str = ''.join( str (b) for b in binary_list)
binary_int = int (binary_str, 2 )
binary_pack = struct.pack( '>I' , binary_int)
decimal = struct.unpack( '>i' , binary_pack)[ 0 ]
print (decimal)
|
Time complexity:
- Converting the binary list to a binary string requires iterating through each element of the list once, which takes O(n) time, where n is the length of the list.
- Converting the binary string to an integer with a base of 2 using the int() function takes O(log n) time, where n is the value of the binary string.
Packing the resulting integer into a binary string using the pack() function takes O(1) time.
- Unpacking the binary string into an integer using the unpack() function takes O(1) time.
- Therefore, the overall time complexity of the approach is O(n + log n), which simplifies to O(log n) since log n is the dominant term
Auxiliary space complexity:
- The approach requires O(n) space to store the binary string, where n is the length of the binary list.
- The approach also requires O(1) space to store the integer value of the binary string and the packed and unpacked binary strings.
- Therefore, the overall auxiliary space complexity of the approach is O(n).
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...