char list from 0 to f

0 votes
I write a python script, which needs a list of all hexadecimal characters.

Is it a good idea to do list(string.printable[:16]) to get ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'] ?
Jul 15 in Python by ana1504.k
• 7,890 points

1 answer to this question.

0 votes

The simplest way would be a list comprehension of all numbers from 0 to 15 formatted as hex:

["{:x}".format(x) for x in range(0,16)]

User GarbageCollector suggested a nice alternative in comments, which must be adapted to remove the redundant, uppercased chars:

>>> import string
>>> string.hexdigits
>>> string.hexdigits[:16]

to get a list:

>>> list(string.hexdigits[:16])
answered Jul 15 by SDeb
• 13,210 points

Related Questions In Python

0 votes
1 answer

How to create Pandas dataframe from Python list?

You can do it like this: import ...READ MORE

answered Apr 5 in Python by Likhita
+1 vote
1 answer

How to convert records from csv to a list in Python?

If you are using Python 3.x then ...READ MORE

answered Jun 25 in Python by Arvind
• 2,960 points

edited Jun 26 by Kalgi 58 views
0 votes
0 answers

How to remove elements from a list that may or may not be present?

In case I want to remove some ...READ MORE

Jun 27 in Python by Nisa
• 1,090 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 399 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,210 points
0 votes
1 answer

How to exit from Python without traceback?

You are presumably encountering an exception and ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,210 points