What does zip mean?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Nov 9 11:58:00 CET 2014


satishmlmlml at gmail.com wrote:

> What does zip return in the following piece of code?

Have you read the Fine Manual?

In Python 2, zip returns a list:

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

=> [('a', 1), ('b', 2), ('c', 3)]

https://docs.python.org/2/library/functions.html#zip


In Python 3, zip does the same, except instead of a list, it returns a
special iterator which produces the items on demand rather than in advance.

https://docs.python.org/3/library/functions.html#zip



> curs.execute('select * from people')
> colnames = [desc[0] for desc in curs.description]
> rowdicts = []
> for row in curs.fetchall():
>            rowdicts.append(dict(zip(colnames, row)))

zip(colnames, row) will return:

(first column name, first item of row),
(second column name, second item of row),
(third column name, third item of row),
etc.



-- 
Steven




More information about the Python-list mailing list