I’ve added a list comprehension into my programming logic cheat sheet. I’m slowly starting picking up Erlang and list comprension has an important role in that language. Honestly, I don’t use list comprehension too frequently but trying to think up an example that would be useful I came up with a script that was pretty fun to play around with.

This example will create a list for each binary base value. There isn’t too much going here, the number 2 will be raised to the power for every number from 0 to 7.

baseDigit = [pow(2,x) for x in range(8)] # [1, 2, 4, 8, 16, 32, 64, 128]

The next example uses another list comprehension to convert each binary base digit into a hex number.

hexDigit = [hex(x) for x in baseDigit] # ['0x1', '0x2', '0x4', '0x8', '0x10', '0x20', '0x40', '0x80']

You can also nest multiple list comprehension if you want to make some cool one liners.

hexDigit = [hex(x) for x in [pow(2,y) for y in range(8)]] # ['0x1', '0x2', '0x4', '0x8', '0x10', '0x20', '0x40', '0x80']

Like I said, I don’t use list comprehension often. Below is another script I’m still working on. My goal was to make a nice one liner that can do a denary to binary base conversion. I haven’t fully figure out how to design it yet but here is logic pulled out. Lets use the denary number 10 as our number to convert using modular division. Sorry this extremely stupid description of how modular division works. I don’t want to sink any more time into this but you should get the picture. If not play around with mod operator in any language and you would figure it out pretty quickly.

10 % 16 (5th base digit) = 10 – If the number on the right is higher, the remainder will always equal the lower number.

10 % 8 (4th base) = 2 – This is a normal case

(10 – 8) % 4 (3th base) = 2

So putting this in an if statement might look like this:

# Didn't test or really prof read this but still you should follow the process. int denary = 10 # This is the base 10 number where going to convert. baseDigit = [pow(2,x) for x in range(8)] # this is our list of binary base digits baseDigit.reverse() # Reverses the list # Note: Off the top of my head I can't think of how you add an item to a list and didn't feel like Googling it for this pseudo code. lst = [] # My list of 1's and 0's. for digit in baseDigit: if (denary != (denary%baseDigit) ) lst.add(1) denary = (denary%baseDigit)) # Setting the denary variable with the remainder else lst.add(0)

The problem I’m running into is how to set the denary variable with remainder of the modular division inside an list comprehension. You can include a tenary if statement into a list comprehension but I’m finding myself unable to alter the denary variable. If your denary variable is still, i = 10, I can’t seem to find a way to a subtract base digit from i and have it remember it. A variable inside an list comprehensions works like an immutable datatype. Here is some code that doesn’t work in the way I would liked.

int denary = 12 # This is the base 10 number where going to convert. binaryList = [denary.__sub__(x) if (denary % x != denary) else 0 for x in [pow(2,abs(y)) for y in range(-7,1)] ] # binaryList = [0, 0, 0, 0, 4, 8, 10, 11]

Update: I’ve got it working with by creating an object. Its still not the elegant solution I’m looking for but it works. Warning though, it’s still super hacky and poorly named.

class denary(): def __init__(self,x): self.x = x def sub(self, x): self.x -= x return 1 def toBinaryList(x): den = denary(x) return( [den.sub(y) if (den.x % y != den.x) else 0 for y in [pow(2,abs(z)) for z in range(-7,1)] ] ) if __name__ == '__main__': print( toBinaryList(10) ) # [0, 0, 0, 0, 1, 0, 1, 0] print( toBinaryList(12) ) # [0, 0, 0, 0, 1, 1, 0, 0]