Feed on

to declare several variables in one line, use multiple equals signs:

var1 = var2 = var3 = var4 = 0

be careful though using this form to declare dicts; due to python’s mutability rules, multiple dictionaries instantiated this way will all point to the same set of keys and values!

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

3 Responses to “declare multiple variables in one line in python”

  1. on 10 May 2008 at 10:03 am 67df4075e33e



  2. on 25 Jun 2012 at 6:04 am Emil

    Another way to do the same thing which allows for assigning different values to each variable is by using the tuples:
    var1, var2, var3, var4 = 0, 0, 0, 0

  3. on 14 May 2016 at 10:37 am J-Doug

    Awesome, thanks!

Did I get this wrong? Let me know!

Trackback URI | Comments RSS