I am trying to insert a list into a list like this:
>>> x = [ 1, 2, 3]
>>> y = [ 2, 4, x , 5]
>>> y
[2, 4, [1, 2, 3], 5]
but what I really want is to have y = [2, 4, 1, 2, 3, 5]. What is the best way to do this?
4 4843
I am trying to insert a list into a list like this:
>>> x = [ 1, 2, 3]
>>> y = [ 2, 4, x , 5]
>>> y
[2, 4, [1, 2, 3], 5]
but what I really want is to have y = [2, 4, 1, 2, 3, 5]. What is the best way to do this?
You can try list concatenation. -
>>> x = [1, 2, 3]
-
>>> y = [2, 4] + x + [5]
-
>>> y
-
[2, 4, 1, 2, 3, 5]
-
You can try list concatenation. -
>>> x = [1, 2, 3]
-
>>> y = [2, 4] + x + [5]
-
>>> y
-
[2, 4, 1, 2, 3, 5]
-
You can also use map: -
>>> x = [1, 2, 3]
-
>>> y = [2, 4]
-
>>> map(y.append, x)
-
>>> y
-
[2, 4, 1, 2, 3]
-
thanks guys, those are great solutions!
bvdet 2,851
Expert Mod 2GB
I am trying to insert a list into a list like this:
>>> x = [ 1, 2, 3]
>>> y = [ 2, 4, x , 5]
>>> y
[2, 4, [1, 2, 3], 5]
but what I really want is to have y = [2, 4, 1, 2, 3, 5]. What is the best way to do this?
Here is one way: - >>> x = [1,2,3]
-
>>> y = [2,4,5]
-
>>> [y.insert(i+2, item) for i,item in enumerate(x)]
-
[None, None, None]
-
>>> y
-
[2, 4, 1, 2, 3, 5]
-
>>>
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by Jonathan Moss |
last post: by
|
1 post
views
Thread by pmud |
last post: by
|
1 post
views
Thread by Scott Chapman |
last post: by
|
3 posts
views
Thread by Joachim Klassen |
last post: by
|
1 post
views
Thread by Sailer, Denis |
last post: by
|
15 posts
views
Thread by John Salerno |
last post: by
|
6 posts
views
Thread by DWrek |
last post: by
| |
5 posts
views
Thread by dos360 |
last post: by
|
reply
views
Thread by Edwin.Madari |
last post: by
| | | | | | | | | | |