Skip to content
Related Articles

Related Articles

Python | Decimal to binary list conversion

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Feb, 2019
View Discussion
Improve Article
Save Article

The conversion of a binary list to a decimal number has been dealt in a previous article. This article aims at presenting certain shorthand to do the opposite, i.e binary to decimal conversion. Let’s discuss certain ways in which this can be done.

Method #1 : Using list comprehension + format()
In this method, the conversion of the decimal to binary is handled by the format function. The logic of conversion to the list is done by the list comprehension function.




# Python3 code to demonstrate 
# decimal to binary number conversion
# using format() + list comprehension
  
# initializing number 
test_num = 38
  
# printing original number
print ("The original number is : " + str(test_num))
  
# using format() + list comprehension
# decimal to binary number conversion 
res = [int(i) for i in list('{0:0b}'.format(test_num))]
  
# printing result 
print ("The converted binary list is : " +  str(res))

Output:

The original number is : 38
The converted binary list is : [1, 0, 0, 1, 1, 0]

 
Method #2 : Using bin() + list comprehension
The inbuilt function bin performs the function of conversion to binary and the list comprehension handles the logic to convert the binary number to the list.




# Python3 code to demonstrate 
# decimal to binary number conversion
# using bin() + list comprehension
  
# initializing number 
test_num = 38
  
# printing original number
print ("The original number is : " + str(test_num))
  
# using bin() + list comprehension
# decimal to binary number conversion 
res = [int(i) for i in bin(test_num)[2:]]
  
# printing result 
print ("The converted binary list is : " +  str(res))

Output:

The original number is : 38
The converted binary list is : [1, 0, 0, 1, 1, 0]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!