dawed1999
dawed1999

Reputation: 335

Not getting the entire list back when converting from binary to decimal

I'm trying to convert integers from my list to binary and vice versa. but for some reason i'm only getting the last integer back and not the entire list.

redChannelData = [46, 49, 50, 51, 53, 53, 54, 56, 59, 59, 60, 61, 62, 62, 64, 64, 65, 65]

for value in redChannelData:
    encoded = [bin(value)[2:]]
    print(encoded)

for binary in encoded:
    decoded = [int(binary ,2)]
    print(decoded)
Output:
['101110']
['110001']
['110010']
['110011']
['110101']
['110101']
['110110']
['111000']
['111011']
['111011']
['111100']
['111101']
['111110']
['111110']
['1000000']
['1000000']
['1000001']
['1000001']
[65]

Upvotes: 0

Views: 50

Answers (2)

LetzerWille
LetzerWille

Reputation: 5658

    with a list-comprehension     

        [bin(i)[2:] for i in redChannelData]  

        Out[32]:    

        ['101110',
         '110001',
         '110010',
         '110011',
         '110101',
         '110101',
         '110110',
         '111000',
         '111011',
         '111011',
         '111100',
         '111101',
         '111110',
         '111110',
         '1000000',
         '1000000',
         '1000001',
         '1000001']

Upvotes: 0

Ravi S
Ravi S

Reputation: 139

for value in redChannelData:
    encoded = [bin(value)[2:]]
    print(encoded)

In the loop above, you are creating a single element list and printing it. When the loop is over, encoded has the last value which is 65. You can create encoded as an empty list and append the binary value in the loop.

Upvotes: 1

Related Questions