Python | Binary list to integer

The problem being discussed in this article is quite common that 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :



The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list is : [1, 0, 0, 1, 1, 0]
The converted integer value is : 38


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.