I have a list:
row = ["Title", "url", 33, "title2", "keyword"]
Is there a more pythonic way to unpack this values like:
title, url, price, title2, keyword = row[0], row[1], row[2], row[3], row[4]
Method #1 : Using "=" operator This task can be performed using a “=” operator. In this we just ensure enough variables as the count of list elements and assign them to list, the list elements get allotted to variables in order of their assignment.
Summary. Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.
To unpack elements of a uint8 array into a binary-valued output array, use the numpy. unpackbits() method in Python Numpy. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done.
Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .
Something like this?
>>> row = ["Title", "url", 33, "title2", "keyword"] >>> title, url, price, title2, keyword = row
Also if you need only few first variables, in Python 3 you can use:
row = ["Title", "url", 33, "title2", "keyword"] title, url, *_ = row
It's a nice way to extract few first values without using explicit indices
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