473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

inserting a list into a list (but not as a list)

4
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?
Aug 10 '07 #1
4 4927
ilikepython
844 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1, 2, 3]
  2. >>> y = [2, 4] + x + [5]
  3. >>> y
  4. [2, 4, 1, 2, 3, 5]
  5.  
Aug 10 '07 #2
ilikepython
844 Expert 512MB
You can try list concatenation.
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1, 2, 3]
  2. >>> y = [2, 4] + x + [5]
  3. >>> y
  4. [2, 4, 1, 2, 3, 5]
  5.  
You can also use map:
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1,  2, 3]
  2. >>> y = [2, 4]
  3. >>> map(y.append, x)
  4. >>> y
  5. [2, 4, 1, 2, 3]
  6.  
Aug 10 '07 #3
POINTS
4
thanks guys, those are great solutions!
Aug 10 '07 #4
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:
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1,2,3]
  2. >>> y = [2,4,5]
  3. >>> [y.insert(i+2, item) for i,item in enumerate(x)]
  4. [None, None, None]
  5. >>> y
  6. [2, 4, 1, 2, 3, 5]
  7. >>> 
Aug 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jonathan Moss | last post by:
I have (almost) succesfully created a linked list data structure class. It will quite happily add items to to the end of the list (once I found that objects are not passed by reference :). What I...
1
by: pmud | last post by:
I have an ASP.NET web application using C# code. I am trying to insert values from a web form into an SQL database. I am using SQL COMMAND object for this. I need to know HOW TO INSERT THE RADIO...
1
by: Scott Chapman | last post by:
I am working with Python (psycopg). I have HTML with embedded Python that I'm inserting into a database and it could contain any character. Single quotes, at least, must be escaped (to two...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
1
by: Sailer, Denis | last post by:
There was a posting in the mailing list archives that I can't find anymore. The web site right now is presenting a list of items from a search in a reasonable amount of time, but takes 5-10 minutes...
15
by: John Salerno | last post by:
Let me apologize in advance for what I'm sure is an achingly simple question, but I just can't find the answer in either of my Python books. I've tried a few tests with the interactive prompt, but...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
14
Parul Bagadia
by: Parul Bagadia | last post by:
Here is the code i hav written for inserting a no., after given no. in a link list; i guess the logic is ofcourse right. there is no error in it, but at the time of display its not displaying the...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.