Skip to main content
Remove permalink because it doesn't exist anymore. Yeah.
Source Link

Python lacks dynamic unpacking. Example: You want to unpack a list, let's say coordinates, but don't know whether it contains 3 items or just 2.

x, y, z = [1,2,3] works only if len([x,y,z]) == len([1,2,3]).

x, y, z = [1,2] results in an error. You could add try and except blocks but that could be complicated.

The best option is z being None, as you can simply check using if n is None without any excessive try/except.

So expected result:

>>> x, y, z = unpack([1,2])
>>> print(x)
1
>>> print(y)
2
>>> print(z)
None

My code [permalink]

def unpack(num, list):
    return_arr = [None] * num
    for i,elem in enumerate(list):
        return_arr[i] = elem
        if i+1 == num:
            return return_arr
    return return_arr

And usage examples:

a,b,c,d = unpack(4, [1,2,3])
print(a),
print(b),
print(c),
print(d),
print("\n")

e,f,g = unpack(3, [1,2,3])
print(e),
print(f),
print(g)

resulting in

1 2 3 None 

1 2 3

You basically have to specify the amount of variables you're unpacking the list to, since the function can't know that.

Python lacks dynamic unpacking. Example: You want to unpack a list, let's say coordinates, but don't know whether it contains 3 items or just 2.

x, y, z = [1,2,3] works only if len([x,y,z]) == len([1,2,3]).

x, y, z = [1,2] results in an error. You could add try and except blocks but that could be complicated.

The best option is z being None, as you can simply check using if n is None without any excessive try/except.

So expected result:

>>> x, y, z = unpack([1,2])
>>> print(x)
1
>>> print(y)
2
>>> print(z)
None

My code [permalink]

def unpack(num, list):
    return_arr = [None] * num
    for i,elem in enumerate(list):
        return_arr[i] = elem
        if i+1 == num:
            return return_arr
    return return_arr

And usage examples:

a,b,c,d = unpack(4, [1,2,3])
print(a),
print(b),
print(c),
print(d),
print("\n")

e,f,g = unpack(3, [1,2,3])
print(e),
print(f),
print(g)

resulting in

1 2 3 None 

1 2 3

You basically have to specify the amount of variables you're unpacking the list to, since the function can't know that.

Python lacks dynamic unpacking. Example: You want to unpack a list, let's say coordinates, but don't know whether it contains 3 items or just 2.

x, y, z = [1,2,3] works only if len([x,y,z]) == len([1,2,3]).

x, y, z = [1,2] results in an error. You could add try and except blocks but that could be complicated.

The best option is z being None, as you can simply check using if n is None without any excessive try/except.

So expected result:

>>> x, y, z = unpack([1,2])
>>> print(x)
1
>>> print(y)
2
>>> print(z)
None

My code

def unpack(num, list):
    return_arr = [None] * num
    for i,elem in enumerate(list):
        return_arr[i] = elem
        if i+1 == num:
            return return_arr
    return return_arr

And usage examples:

a,b,c,d = unpack(4, [1,2,3])
print(a),
print(b),
print(c),
print(d),
print("\n")

e,f,g = unpack(3, [1,2,3])
print(e),
print(f),
print(g)

resulting in

1 2 3 None 

1 2 3

You basically have to specify the amount of variables you're unpacking the list to, since the function can't know that.

Source Link

Dynamic unpacking in Python

Python lacks dynamic unpacking. Example: You want to unpack a list, let's say coordinates, but don't know whether it contains 3 items or just 2.

x, y, z = [1,2,3] works only if len([x,y,z]) == len([1,2,3]).

x, y, z = [1,2] results in an error. You could add try and except blocks but that could be complicated.

The best option is z being None, as you can simply check using if n is None without any excessive try/except.

So expected result:

>>> x, y, z = unpack([1,2])
>>> print(x)
1
>>> print(y)
2
>>> print(z)
None

My code [permalink]

def unpack(num, list):
    return_arr = [None] * num
    for i,elem in enumerate(list):
        return_arr[i] = elem
        if i+1 == num:
            return return_arr
    return return_arr

And usage examples:

a,b,c,d = unpack(4, [1,2,3])
print(a),
print(b),
print(c),
print(d),
print("\n")

e,f,g = unpack(3, [1,2,3])
print(e),
print(f),
print(g)

resulting in

1 2 3 None 

1 2 3

You basically have to specify the amount of variables you're unpacking the list to, since the function can't know that.