Then we use a list comprehension to create a list of 'n' elements:
>>> range(4) # range create a list of 'n' numbers
[0, 1, 2, 3]
>>> ['elem' for _ in range(4)] # we use range to create 4 times 'elem'
['elem', 'elem', 'elem', 'elem']
In the example above, we use [ to create the list, but we don't in the id_generator function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).
Instead of asking to create 'n' times the string elem, we will ask Python to create 'n' times a random character, picked from a sequence of characters:
Therefore random.choice(chars) for _ in range(size) really is creating a sequence of size characters. Characters that are randomly picked from chars:
>>> [random.choice('abcde') for _ in range(3)]
['a', 'b', 'b']
>>> [random.choice('abcde') for _ in range(3)]
['e', 'b', 'e']
>>> [random.choice('abcde') for _ in range(3)]
['d', 'a', 'c']
Then we just join them with an empty string so the sequence becomes a string:
>>> ''.join(['a', 'b', 'b'])
'abb'
>>> [random.choice('abcde') for _ in range(3)]
['d', 'c', 'b']
>>> ''.join(random.choice('abcde') for _ in range(3))
'dac'
Best Answer
Answer in one line:
or even shorter starting with Python 3.6 using
random.choices()
:A cryptographically more secure version: see this post
In details, with a clean function for further reuse:
How does it work ?
We import
string
, a module that contains sequences of common ASCII characters, andrandom
, a module that deals with random generation.string.ascii_uppercase + string.digits
just concatenates the list of characters representing uppercase ASCII chars and digits:Then we use a list comprehension to create a list of 'n' elements:
In the example above, we use
[
to create the list, but we don't in theid_generator
function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).Instead of asking to create 'n' times the string
elem
, we will ask Python to create 'n' times a random character, picked from a sequence of characters:Therefore
random.choice(chars) for _ in range(size)
really is creating a sequence ofsize
characters. Characters that are randomly picked fromchars
:Then we just join them with an empty string so the sequence becomes a string: