Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!newsfeed.tele2net.at!news-hub.siol.net!newsfeed.t-com.hr!garrison.bnet.hr!gregory.BNet.hr!busola.homelinux.net!not-for-mail From: Hrvoje Niksic Newsgroups: comp.lang.python Subject: Re: unzip function? Date: Wed, 18 Jan 2012 19:01:10 +0100 Organization: B.net Hrvatska d.o.o. Lines: 41 Message-ID: <87aa5khoux.fsf@xemacs.org> References: NNTP-Posting-Host: cpe-109-60-90-39.zg3.cable.xnet.hr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: gregory.bnet.hr 1326910502 21776 109.60.90.39 (18 Jan 2012 18:15:02 GMT) X-Complaints-To: abuse@globalnet.hr NNTP-Posting-Date: Wed, 18 Jan 2012 18:15:02 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) Cancel-Lock: sha1:vydqhLgdLMnioXGFlSIDblxbtxA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19097 Neal Becker writes: > python has builtin zip, but not unzip > > A bit of googling found my answer for my decorate/sort/undecorate problem: > > a, b = zip (*sorted ((c,d) for c,d in zip (x,y))) > > That zip (*sorted... > > does the unzipping. > > But it's less than intuitively obvious. > > I'm thinking unzip should be a builtin function, to match zip. "zip" and "unzip" are one and the same since zip is inverse to itself: >>> [(1, 2, 3), (4, 5, 6)] [(1, 2, 3), (4, 5, 6)] >>> zip(*_) [(1, 4), (2, 5), (3, 6)] >>> zip(*_) [(1, 2, 3), (4, 5, 6)] >>> zip(*_) [(1, 4), (2, 5), (3, 6)] What you seem to call unzip is simply zip with a different signature, taking a single argument: >>> def unzip(x): ... return zip(*x) ... >>> [(1, 2, 3), (4, 5, 6)] [(1, 2, 3), (4, 5, 6)] >>> unzip(_) [(1, 4), (2, 5), (3, 6)] >>> unzip(_) [(1, 2, 3), (4, 5, 6)] >>> unzip(_) [(1, 4), (2, 5), (3, 6)]