My background is in C, and I'm finally learning this new-fangled "Python" that all the cool kids are talking about.
I want to create a list of formatted hexadecimal values like so:
['0x00','0x01','0x02','0x03' (...) '0xfe','0xff']
Please note that the leading zero's shouldn't be stripped. For example, I want
My first successful attempt was this:
hexlist=list() tempbytes = bytes(range(256)) for value in tempbytes: hexlist.append("0x" + tempbytes[value:value+1].hex()) del tempbytes
But, wow this is ugly. Then I tried to make it more Pythonic ("Pythony"?) like so:
hexlist = ["0x"+bytes(range(256))[x:x+1].hex() for x in bytes(range(256))]
- OMG! That's harder to read, not easier!
- Not only that, but I had to invoke
rangetwice, which I assume is inefficient.
- I bet there's a much better way that I haven't been exposed to yet...
- Is the second much less efficient?
- Is there a better way to do this?
- What's the best thing to do here in keeping with python style?