what does zip(*res) mean in python in the following code?

Viewed 4983

Here is a piece of code from Allen Downey's Think Bayes book on the github:

def ReadData(filename='showcases.2011.csv'):
    """Reads a CSV file of data.
    Args:
      filename: string filename
    Returns: sequence of (price1 price2 bid1 bid2 diff1 diff2) tuples
    """
    fp = open(filename)
    reader = csv.reader(fp)
    res = []

    for t in reader:
        _heading = t[0]
        data = t[1:]
        try:
            data = [int(x) for x in data]
            # print heading, data[0], len(data)
            res.append(data)
        except ValueError:
            pass

    fp.close()
    return zip(*res)

The entire file can be seen at this URL:link on Github for this file.

I am trying to figure out what does zip(*res) mean on the last line of code? Specifically:

  1. What does a '*' do when used as a prefix. and next
  2. What does the zip function do to (*anything)

I am new to Python, so I may be asking something obvious. I see the author's note in the function's docstring, that it returns a sequence of (price1 price2 ...), but it is less than clear to me.

UPDATE: Following up on James Rettie's anwer, here is what I get when I run the code he provided in Python 3.6:

In [51]: zip(['a', 'b', 'c'], [1, 2, 3])
Out[51]: <zip at 0x1118af848>

Whereas running the same code in Python 2.7 yields the results that he provided as shown below:

In [2]: zip(['a', 'b', 'c'], [1, 2, 3])
Out[2]: [('a', 1), ('b', 2), ('c', 3)]

Can you explain why? The difference is Python 2.7 and Python 3.6 is important to me since I have to still support Python 2.7, but I would like to move to 3.6.

2 Answers
Related