I have a list of all possible combinations of a 4-digit number and I want to put them in possible_comb = […]
But the list does not have any separator like a comma for example and it does not have the “..” per 4 digit number.
Example:
#This is my list
JavaScript
x
2
1
list = [ 0000 0001 0002 0003...9999] # no (,) and (")
2
#expected
JavaScript
1
2
1
list = ["0000", "0001", "0002","9999"]
2
Putting ” and , manually is hellish. How to put “…” and , on every string from the list? Or are there any other ways to list all possible 4 digit numbers and put it in a list as a string?Thanks.
Advertisement
Answer
You can make a list like this with a list comprehension by converting integers to strings and using zfill()
to pad with zeros:
JavaScript
1
2
1
l = [str(n).zfill(4) for n in range(10000)]
2
l
will be:
JavaScript
1
10
10
1
['0000',
2
'0001',
3
'0002',
4
'0003',
5
'0004',
6
'0005',
7
8
'9999'
9
]
10
Depending on how you are using it you might be better off with a generator expression.
JavaScript
1
2
1
l = (str(n).zfill(4) for n in range(10000))
2