Skip to content
Advertisement

Python: how to put ” ” on every string from a list

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

#expected

JavaScript

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

l will be:

JavaScript

Depending on how you are using it you might be better off with a generator expression.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement