We're unpacking each of the values in our tuple above into separate variable names. You can think of creating a tuple as packing values together and unpacking a tuple as undoing that work. I won’t share you info with others (see the Python Morsels Privacy Policy for details). We've taken a three-item tuple and unpacked it into three variables ( x, y, and z ): > print(x, y, z) 2 1 3. If you sign up to Python Morsels using this form, I’ll immediately send you an exercise that involves tuple unpacking. To get practice writing some readable code using tuple unpacking, sign up for Python Morsels using the form below. You don’t learn by reading articles like this one, you learn by writing code. Please feel free to use this article as your personal reference guide to multiple assignment. It’s tricky to recognize and remember all the cases that multiple assignment can come in handy. You can unpack a list or a string with the same syntax. ![]() It’s also common to overlook the fact that multiple assignment works deeply and can be used with both a tuple-like syntax and a list-like syntax. In Python, unpacking is not limited to tuples only. The use for multiple assignment that I often see forgotten is its ability to replace hard coded indexes, including replacing hard coded slices (using the * syntax). It can make your code more descriptive while also making implicit assertions about the size and shape of the iterables you’re unpacking. Multiple assignment can improve both the readability of your code and the correctness of your code. When using multiple assignment in your code, consider when and where a list-like syntax might make your code more descriptive and more clear. I don’t usually use this convention myself, mostly because I’m just not in the habit of using it.īut if you find it helpful, you might consider using this convention in your own code. If you’d like you might even decide to adopt a convention of always using a list-like syntax when unpacking list-like structures (frequently the case when using * in multiple assignment): When you’re unpacking structures that often hold lots of values (like lists) and structures that often hold a very specific number of values (like tuples) you may decide that your code appears more semantically accurate if you use a list-like syntax when unpacking those list-like structures. We’re limiting the list we’re getting back to just a single item. The most_common method on Counter objects allows us to limit the length of the list returned to us. We’re using a Counter object from the collections module here. When I see this, I also think I bet we’re unpacking a single-item list. Unpacking tuples means assigning individual elements of a tuple to multiple variables. The list-syntax in our assignment makes it more clear that we’re unpacking a one-item iterable and then unpacking that single item into value and times_seen variables. Python uses the commas (, ) to define a tuple, not parentheses. Python’s multiple assignment looks like this:ĭef most_common ( items ): = Counter ( items ). They’re all just different words for the same thing. Python uses a special syntax to pass optional arguments (args) for tuple unpacking. I’ll be using the words multiple assignment, tuple unpacking, and iterable unpacking interchangeably in this article. If you’re on an older version of Python, you’ll need to mentally translate those to use the string format method. Note that in this article I will be using f-strings which are a Python 3.6+ feature. In this article we’ll see what multiple assignment is, we’ll take a look at common uses of multiple assignment, and then we’ll look at a few uses for multiple assignment that are often overlooked. ![]() ![]() This feature often seems simple after you’ve learned about it, but it can be tricky to recall multiple assignment when you need it most. Multiple assignment (also known as tuple unpacking or iterable unpacking) allows you to assign multiple variables at the same time in one line of code. Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercise Python If.Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Scope Python Modules Python Dates Python Math Python JSON Python RegEx Python PIP Python Try.Whether I’m teaching new Pythonistas or long-time Python programmers, I frequently find that Python programmers underutilize multiple assignment.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |