I want to create some alphanumeric strong passwords in python. Some available ways are:
import string
from random import sample, choice
chars = string.letters + string.digits
length = 8
''.join(sample(chars,length)) # way 1
''.join([choice(chars) for i in range(length)]) # way 2
I found this way to do this in an online [[ https://intellipaat.com/blog/tutorial/python-tutorial/ | python tutorial ]]. Any other good options?
The below timeit for 100000 iterations:
''.join(sample(chars,length)) # way 1; 2.5 seconds
''.join([choice(chars) for i in range(length)]) # way 2; 1.8 seconds (optimizer helps?)
''.join(choice(chars) for _ in range(length)) # way 3; 1.8 seconds
''.join(choice(chars) for _ in xrange(length)) # way 4; 1.73 seconds
''.join(map(lambda x: random.choice(chars), range(length))) # way 5; 2.27 seconds
So, the winner is ''.join(choice(chars) for _ in xrange(length)).