Unpack operator * in Python

Juno Nguyen
3 min readAug 12, 2018

--

As you already know that the operator * (asterisk) is not only used for multiplication and replication, but also for unpacking. Information about it is very rare in the Internet. Because of that, I will explain it for you now.

The * operator

If you place it to the left of the object, list, dictionary, etc… It will produce individual elements of the iterable.

If applied on a list-like iterable, it produces the elements of the list in the order they are sorted in the list.

If applied on a dict-like object, it produces the keys of the dict in the order you would get as if you iterated the dict

def foo(x, y, z):print("First is ", x, " then ", y, " lastly ", z)a = [1, 50, 99]foo(a)# TypeError: foo() takes exactly 3 arguments (1 given)foo(*a)# First is 1 then 50 lastly 99b = [[55,66,77], 88, 99]foo(*b)# First is [55,66,77] then 88 lastly 99d = {"y": 23, "z": 56, "x": 15}foo(*d)# This passes in the keys of the dict# First is z then x lastly y

The ** operator

In the same fashion, dictionaries can deliver keyword arguments with the ** operator.

This ** operator makes it easy to pass a dictionary to a function that has keyword arguments. Just make sure that the number of keys and the name of the keys in the dict exactly match the number and names of the function parameters. If the number of keys is less or more than in the function parameters or if some keys do not match the function parameter names, then the function call fails.

def foo(x, y, z):print("First is ", x, " then ", y, " lastly ", z)d = {"y": 23, "z": 56, "x": 15}foo(d)# TypeError: foo() takes exactly 3 arguments (1 given)foo(*d)# Works, but not what you wanted# First is z then x lastly yfoo(**d)# First is 56 then 15 lastly 23

Example

Task

Dr. John Wesley has a spreadsheet containing a list of student’s IDs, marks, class and name.

Your task is to help Dr. Wesley calculate the average marks of the students.

Note:
1. Columns can be in any order. IDs, marks, class and name can be written in any order in the spreadsheet.
2. Column names are
ID, MARKS, CLASS and NAME. (The spelling and case type of these names won't change.)

Input Format

The first line contains an integer , the total number of students.
The second line contains the names of the columns in any order.
The next lines contains the IDs, marks, class and name, under their respective column names.

Output Format

Print the average marks of the list corrected to 2 decimal places.

Sample Input

TESTCASE 01

5
ID MARKS NAME CLASS
1 97 Raymond 7
2 50 Steven 4
3 91 Adrian 9
4 72 Stewart 5
5 80 Peter 6

TESTCASE 02

5
MARKS CLASS NAME ID
92 2 Calum 1
82 5 Scott 2
94 2 Jason 3
55 8 Glenn 4
82 2 Fergus 5

Sample Output

TESTCASE 01

78.00

TESTCASE 02

81.00

Solution here

from collections import namedtuple
n, Student = int(input()), namedtuple(‘Student’, input())
print(“%.2f” %( sum([float(stud.MARKS) for stud in [Student(*input().split()) for _ in range(n)]]) / n ))

Reference

  1. https://codeyarns.com/2012/04/26/unpack-operator-in-python/
  2. https://docs.python.org/3/tutorial/controlflow.html
  3. Example created by author DOSHI

--

--

Juno Nguyen

Master of Computer Science and Engineering, Volgograd State Technical University