I have a few situations where i am taking a list of raw data and am passing it into a class. At present it looks something like this:
x = Classname(
listname[0],
listname[1],
listname[2],
listname[3],
listname[4],
listname[5],
listname[6],
listname[7],
...
)
and so on. This is quite long and frustrating to read, especially when i am doing it multiple times in the same file, so i was wondering if there was a simpler way to write this? Something to the effect of:
x = Classname(
# item for item in list
)
Any help would be appreciated, my brain is fried. Cheers.
Unpack the list with the *args
notation.
x = Classname(*listname)
You could use
listname = [1, 2, 3, 4, 5]
class Classname:
def __init__(self, *args):
print(args)
x = Classname(*listname)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With