Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34262
| Date | 2012-12-04 17:50 -0500 |
|---|---|
| From | Dave Angel <d@davea.name> |
| Subject | Re: assign only first few items of a tuple/list |
| References | <CADjSo4TP03MTF_8Htn-N4xNV1zB08wTK7geLvxU0jYX_FXUqmg@mail.gmail.com> <CAPTjJmr+UB0phzuWoNYQxo8R3CQrVgMsH=vUf26MV7c+P+uKtg@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.482.1354661449.29569.python-list@python.org> (permalink) |
On 12/04/2012 04:36 PM, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson > <fetchinson@googlemail.com> wrote: >> Hi folks, I swear I used to know this but can't find it anywhere. >> Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. >> I'd like to assign the first two items to two variables, something like, >> >> a, b, _ = x >> >> but the above will not work, of course, but what is the common idiom >> for this that does? > Try this: > > a, b, *_ = x > > Assigns 1 to a, 2 to b, and [3,4,5] to _ > If you're on Python 3.x. If you're on 2.x, you need to do a slice notation like Terry points out. -- DaveA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: assign only first few items of a tuple/list Dave Angel <d@davea.name> - 2012-12-04 17:50 -0500
csiph-web