Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unpack optional items from a tuple? [duplicate]

I have a list of some input values, of which the first couple of mandatory and the last couple optional. Is there any easy way to use tuple unpacking to assign these to variables, getting None if the optional parameters are missing.

eg.

a = [1,2]   
foo, bar, baz = a
# baz == None

ideally a could be any length - including longer than 3 (other items thrown away).

At the moment I'm using zip with a list of parameter names to get a dictionary:

items = dict(zip(('foo', 'bar', 'baz'), a))
foo = items.get('foo', None)
bar = items.get('bar', None)
baz = items.get('baz', None)

but it's a bit long-winded.

like image 967
xorsyst Avatar asked Jan 31 '14 15:01

xorsyst


People also ask

Is unpacking possible in a tuple?

In python tuples can be unpacked using a function in function tuple is passed and in function, values are unpacked into a normal variable. The following code explains how to deal with an arbitrary number of arguments. “*_” is used to specify the arbitrary number of arguments in the tuple.

How do you extract tuple values?

Python uses a special syntax to pass optional arguments (*args) for tuple unpacking. This means that there can be many number of arguments in place of (*args) in python. All values will be assigned to every variable on the left-hand side and all remaining values will be assigned to *args .

What is unpacking of tuple?

Unpacking a tuple means splitting the tuple's elements into individual variables. For example: x, y = (1, 2) Code language: Python (python)

Does tuple unpacking work with lists?

In Python, unpacking is not limited to tuples only. You can unpack a list or a string with the same syntax. Unpacking is more commonly known as multiple assignment, as it reminds of assigning multiple variables on the same line.

How to unpack every single item in a tuple?

Sometimes, you don’t want to unpack every single item in a tuple. For example, you may want to unpack the first and second elements. In this case, you can use the * operator. For example: In this example, Python assigns 192 to r, 210 to g. Also, Python packs the remaining elements 100 and 0.5 into a list and assigns it to the other variable.

What is the difference between tuple packing and unpacking in Python?

In packing, we put values into a new tuple while in unpacking we extract those values into a single variable. NOTE : In unpacking of tuple number of variables on left-hand side should be equal to number of values in given tuple a. Python uses a special syntax to pass optional arguments (*args) for tuple unpacking.

How to remove items out of a Python tuple?

If you want to remove items out of a Python tuple, you can use index slicing to leave out a particular index. For example, This will give the output: Or you can convert it to a list, remove the item and convert back to a tuple.

What are tuples in Python?

Python Tuples In python tuples are used to store immutable objects. Python Tuples are very similar to lists except to some situations. Python tuples are immutable means that they can not be modified in whole program.


1 Answers

From the linked question, this works:

foo, bar, baz == (list(a) + [None]*3)[:3]
like image 124
xorsyst Avatar answered Sep 30 '22 08:09

xorsyst