0

I have a list, for example ['exa', 'mp', 'l', 'e'].

I need to work with each element to hexlify it. How do I want to do it? At first, I want to get each element somehow and then do hex(element). How do I get it? Or may be I can just hexlify the whole list?

Thanks in advance.

I want to say that I tried str(listname), but it just outputs "['exa', 'mp', 'l', 'e']" which doesn't fulfill my expectations.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Please update your question with the code you have tried. – quamrana May 14 '20 at 13:57
  • I haven't tried anything but str(list), it doesn't work –  May 14 '20 at 13:58
  • 1
    What do you mean under `hex(element)`? Python's built-in `hex` function expects integer number but your element are `str`s. – Daweo May 14 '20 at 13:58
  • Well, that's precisely what we want to see. Please update the question with this code and the output it produces and a reason why this doesn't fulfill your expectations. – quamrana May 14 '20 at 14:00
  • Ok, that's better. So about those expectations, what are they exactly? – quamrana May 14 '20 at 14:02
  • I want to see "exa", "mp", "l", "e" as, like, different variables or anything. But also as another option I want to hexlify the whole list and then separate it in variables like "hexvar", "otherhexvar", "lasthexvar" –  May 14 '20 at 14:04
  • Hmm, that's not very clear. Any way what exactly is `hexlify`? Also the `separate it in variables` seems a lot like [this](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) which is a no-no. – quamrana May 14 '20 at 14:07
  • Hexlify is to make it in hex. Example: transform "exa", "mp", "l", "e" into "657861", "6d70", "6c", "65" –  May 14 '20 at 14:09

2 Answers2

5

Use list comprehension:

result = [hex(x) for x in arr]
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
1

The hexlify thing you are asking for can be done like this:

listname = ['exa', 'mp', 'l', 'e']
print([item.encode('utf-8').hex() for item in listname])

Output:

['657861', '6d70', '6c', '65']
quamrana
  • 37,849
  • 12
  • 53
  • 71