Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101300 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2016-01-06 14:56 +0100 |
| Last post | 2016-01-06 14:56 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: insert many numbers to a list, a second method. Peter Otten <__peter__@web.de> - 2016-01-06 14:56 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-06 14:56 +0100 |
| Subject | Re: insert many numbers to a list, a second method. |
| Message-ID | <mailman.29.1452088595.2305.python-list@python.org> |
飛飛 wrote: > l = list(range(0,12)) > numbers = [5,3,2,7] #insert numbers at 5th position. > list1 = list(range(5,9)) > list2 = list(range(0,5)) > list2.extend(numbers) # > for i in list1: > l.insert(i,list2[i]) > print(l)------> l = [0, 1, 2, 3, 4, 5, 3, 2, 7, 5, 6, 7, 8, 9, > 10, 11] Sorry, I cannot make sense of your sample code. If this is homework and your assignment is to find another way to insert items into a list have a look at slices. Example to get items three to five *out* of the list: >>> items [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> items[2:5] [2, 3, 4] Size zero is legal, too: >>> items[7:7] [] Assigning is similar, and the size of the slice on the left doesn't have to be the same as that of the list on the right.
Back to top | Article view | comp.lang.python
csiph-web