DEV Community


Discussion on: Python tips: unpack data gracefully

v_it_aly profile image
Vitaly Shchurov Author • Edited

About using starred expressions alone. Yes, it's possible. For example, instead of assigning a variable to a list directly, you can do it with a range():

*data, = range(1, 11)
Enter fullscreen mode Exit fullscreen mode

This will produce a list of numbers from 1 to 10. You need to use comma, otherwise you'll get a range object instead of a list. It happens because Python gets comma as a syntax for creating a tuple, not (), so it's a syntactic rule that you cannot create a tuple of one item without using a comma. So, this (1) is an integer for Python, and this (1,) or this 1, is a tuple.