Related Articles

Related Articles

Python | Decimal to binary list conversion
  • Last Updated : 26 Feb, 2019

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :