Convert Binary List to Decimal & Vice Versa in Python (Examples)
Hi! This short tutorial will show you how to transform a list of binaries to decimals and vice-versa in the Python programming language.
First, though, here is a quick overview of this tutorial:
Let’s get right into the Python code!
Create Sample Binary List
Here, we will create the sample binary list that will be turned into a decimals list in this tutorial. So, in your preferred Python coding IDE, run the line of code below to create the sample list of binaries.
binary_list = ["100100","110000","101100","111111"] print(binary_list) # ['100100', '110000', '101100', '111111']
Example 1: Binary to Decimal | Transform Binary List to Decimal List Using a For Loop & int() Function
In this example, we will transform the list of binaries, binary_list, to a list of decimals using a for loop and Python’s built-in int() function.
decimal_list = [] for i in binary_list: decimal_list.append(int(i,2)) print(decimal_list) # [36, 48, 44, 63]
In the code above, we used the for loop to run an iteration through the binaries in binary_list and parsed that to the int() function, where we gave the number “2” to indicate that we are converting a binary number. We then appended them to “decimal_list”.
Using the int()
function is the easiest way to turn a binary list to decimal in the Python programming language. Let us now examine how to transform the decimals back into binaries.
Example 1: Decimal to Binary | Transform Decimal List to Binary List Using map() & bin() Functions
In this example, we will combine Python’s map() and bin() functions to turn the decimals back to binaries in the list.
bin_list = list(map(bin, decimal_list)) print(bin_list) # ['0b100100', '0b110000', '0b101100', '0b111111']
In the above example, we used the map()
function to iterate through the decimals list and parsed that iteration to the bin()
function, which converted the decimals to binary values. Then with the list() function, we packed them in a list. Very simple!
With that, we have demonstrated in this short tutorial how to convert a binary list to decimal and vice-versa in Python. I hope you found it helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a binary list to decimal and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a binary list to decimal and vice-versa in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Check if List of Lists is Empty in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Convert List from Boolean to Float in Python (2 Examples)
- Learn Python Programming
- Convert List to Matrix & Vice-Versa in Python (Examples)
This post has shown how to convert a binary list to decimal and vice-versa in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter