Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69941
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: how to insert the elements in a list properly? |
| Date | 2014-04-09 12:46 +0200 |
| Organization | None |
| References | <CAOmh460uYJXX70+xrPYJi6LoBh_fwyrAqfSac+78D3dWYd=72g@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9067.1397040407.18130.python-list@python.org> (permalink) |
length power wrote: > word=["x1","x2","x3","x4","x5"] > w=word[-2:] > del word[-2:] > word.insert(2,w) > word > ['x1', 'x2', ['x4', 'x5'], 'x3'] > > what i want to get is > ['x1', 'x2', 'x4', 'x5', 'x3'] > > how can i insert them ? Make the left-hand side an empty slice: >>> words = ["x1", "x2", "x3", "x4", "x5"] >>> w = words[-2:] >>> del words[-2:] >>> words[2:2] = w >>> words ['x1', 'x2', 'x4', 'x5', 'x3'] Or note that the operation is equivalent to moving the third item to the end: >>> words = ["x1", "x2", "x3", "x4", "x5"] >>> words.append(words.pop(2)) >>> words ['x1', 'x2', 'x4', 'x5', 'x3']
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to insert the elements in a list properly? Peter Otten <__peter__@web.de> - 2014-04-09 12:46 +0200
csiph-web