1

If I have a list of binary

a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', 
   '0b11110000', '0b01011010', '0b10010110', '0b00011110']

I'd like to convert all the string elements into integers and put it back nicely to list of binary this time:

a=[0b11001000, 0b01001100, 0b00111100, 0b00011111, 
   0b11110000, 0b01011010, 0b10010110, 0b00011110]

What shall I do?

3 Answers 3

6

Use int with base 2:

>>> a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', 
...        '0b11110000', '0b01011010', '0b10010110', '0b00011110']
>>> [int(x, 2) for x in a]
[200, 76, 60, 31, 240, 90, 150, 30]
Sign up to request clarification or add additional context in comments.

Comments

5

Give this a try:

a = [int(x, 2) for x in a]

Comments

4

Though in your second list you express the integers as binary, they are still of type int. You can convert the strings, however:

a = [int(x, 2) for x in a]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.