đź’¬ Question: Given a Python list of hexadecimal strings such as `['ff', 'ef', '0f', '0a', '93']`. How to convert it to a list of integers in Python such as `[255, 239, 15, 10, 147]`?

The easiest way to convert a list of hex strings to a list of integers in Python is the list comprehension statement `[int(x, 16) for x in my_list]` that applies the built-in function `int()` to convert each hex string to an integer using the hexadecimal base `16`, and repeats this for each hex string `x` in the original list.

Hereâ€™s a minimal example:

my_list = [‘ff’, ‘ef’, ‘0f’, ‘0a’, ’93’] my_ints = [int(x, 16) for x in my_list] print(my_ints) # [255, 239, 15, 10, 147]

The list comprehension statement applies the expression `int(x, 16)` to each element `x` in the list `my_list` and puts the result of this expression in the newly-created list.

The `int(x, 16)` expression converts a hex string to an integer using the hexadecimal base argument `16`. A semantically identical way to write this would be `int(x, base=16)`.

In fact, there are many more ways to convert a hex string to an integerâ€”each of them could be used in the expression part of the list comprehension statement.

However, Iâ€™ll show you one completely different approach to solving this problem without listing each and every combination of possible solutions. đź‘‡

For Loop with List Append

You can create an empty list and add one hex integer at a time in the loop body after converting it from the hex string `x` using the `eval('0x' + x)` function call. This first creates a hexadecimal string with `'0x'` prefix using string concatenation and then lets Python evaluate the string as if it was real code and not a string.

Hereâ€™s an example:

my_list = [‘ff’, ‘ef’, ‘0f’, ‘0a’, ’93’] my_ints = [] for x in my_list: my_ints.append(eval(‘0x’ + x)) print(my_ints) # [255, 239, 15, 10, 147]

You use the fact that Python automatically converts a hex value of the form `0xff` to an integer `255`:

>>> 0xff 255 >>> 0xfe 254 >>> 0x0f 15

You may want to check out my in-depth guide on this important function for our solution:

đźŚŤ Recommended Tutorial: Python `eval()`

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. Heâ€™s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. Heâ€™s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

You are watching: Easiest Way to Convert List of Hex Strings to List of Integers. Info created by GBee English Center selection and synthesis along with other related topics.