Open In App

Python | Binary list to integer

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# converting binary list to integer
# using join() + list comprehension
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using join() + list comprehension
# converting binary list to integer
res = int("".join(str(x) for x in test_list), 2)
 
# printing result
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




# Python3 code to demonstrate
# converting binary list to integer
# using bit shift + | operator
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using bit shift + | operator
# converting binary list to integer
res = 0
for ele in test_list:
    res = (res << 1) | ele
 
# printing result
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




# Python3 code to demonstrate
# converting binary list to integer
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
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
 
# printing result
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
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# function to combine two elements of the list
 
 
def combine(x, y):
    return 2 * x + y
 
 
# using reduce() to convert binary list to integer
res = reduce(combine, test_list)
 
# printing result
print("The converted integer value is : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


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




# Python3 code to demonstrate
# converting binary list to integer
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
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
 
# printing result
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:

  1. Convert the binary list to a string using join() method.
  2. Use int() function to convert the binary string to an integer.

Python3




# Python3 code to demonstrate
# converting binary list to integer
 
# initializing list
test_list = [1, 0, 0, 1, 1, 0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# convert binary list to string
binary_string = ''.join(map(str, test_list))
 
# convert binary string to integer
res = int(binary_string, 2)
 
# printing result
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:

  1. Initialize the binary list test_list.
  2. Reverse the test_list using the reversed() function and iterate over its elements along with their index using the enumerate() function.
  3. For each element in the reversed test_list, multiply it with 2 raised to the power of its index.
  4. Sum up all the products obtained in step 3 to get the decimal equivalent of the binary number.
  5. Print the decimal equivalent obtained in step 4.

Python3




# Initialize the binary list
test_list = [1, 0, 0, 1, 1, 0]
 
# Calculate the decimal value by summing the product of each bit with its position value
# Reversing the list to start at the least significant bit
res = sum(x * 2**i for i, x in enumerate(reversed(test_list)))
 
# Print the result
print(res)


Output

38

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:

  1. 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.
  2. Convert the binary string to an integer with a base of 2 using the int() function.
  3. 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.
  4. Unpack the resulting binary string using the unpack() function with a format string of ‘>i’, which specifies a big-endian signed integer.
  5. Extract the integer value from the resulting tuple and store it in a variable.

Python3




import struct
 
# Define binary list
binary_list = [1, 0, 0, 1, 1, 0]
 
# Convert binary list to binary string
binary_str = ''.join(str(b) for b in binary_list)
 
# Convert binary string to integer
binary_int = int(binary_str, 2)
 
# Pack integer into binary string
binary_pack = struct.pack('>I', binary_int)
 
# Unpack binary string into integer
decimal = struct.unpack('>i', binary_pack)[0]
 
# Print resulting integer
print(decimal)


Output

38

Time complexity:

  1. 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.
  2. 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.
  3. Unpacking the binary string into an integer using the unpack() function takes O(1) time.
  4. 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:

  1. The approach requires O(n) space to store the binary string, where n is the length of the binary list.
  2. The approach also requires O(1) space to store the integer value of the binary string and the packed and unpacked binary strings.
  3. Therefore, the overall auxiliary space complexity of the approach is O(n).


Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads