Can Python set these variables at once?

Viewed 85

I have these initializations:

countrys = []
codes = []
index_countrys = {}
index_codes = {}

I want to declare them at once like this:

countrys,codes = []
index_countrys,index_codes = {}

Is that allowed in Python?

3 Answers

You can use:

countries, codes = [], []
index_countries, index_codes = {}, {}

or even :

countries, codes, index_countries, index_codes = [], [], {}, {}

This is a good way to set multiple variables to distinct values using only a single line of code.

Searching ā€œpython set multiple variables site:stackoverflow.comā€ will give you many other options and recommendations on which ones to use.

countrys, code = [], []
index_countrys, index_codes = {}, {}

Yes, you can!

On python you can declare chained vars with equals sign. Just like that:

countrys = codes = []
index_countrys = index_codes = {}
Related