473,413 Members | 1,799 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,413 software developers and data experts.

Copying the list elements

440 256MB
Hi,

I am trying to assign the list elements(l1) to another list (l2).When I am trying to remove the element from l2,it is removing the element from l1.

Is there any method available to copy the list elements?So that I can avoid deleting the elements.

Expand|Select|Wrap|Line Numbers
  1. Sample
  2. >>> l1 = [1,2,3,4]
  3. >>> l2 = l1
  4. >>> l2.remove(3)
  5. >>> l1
  6. [1, 2, 4]
  7. >>> l2
  8. [1, 2, 4]
  9. >>> 
  10.  
O/P :

I need l1 = [1,2,3,4] and l2 = [1, 2, 4]
Thanks
PSB
May 6 '07 #1
2 2258
psbasha
440 256MB
>>> l1 = [1,2,3,4]
>>> from copy import copy
>>> l2 = copy(l1)
>>> l2.remove(3)
>>> l1
[1, 2, 3, 4]
>>> l2
[1, 2, 4]
>>>

The above statement works.But is there any other method available

-PSB
May 6 '07 #2
bartonc
6,596 Expert 4TB
>>> l1 = [1,2,3,4]
>>> from copy import copy
>>> l2 = copy(l1)
>>> l2.remove(3)
>>> l1
[1, 2, 3, 4]
>>> l2
[1, 2, 4]
>>>

The above statement works.But is there any other method available

-PSB
>>> l1 = [1, 2, 3, 4]
>>> l2 = l1[:] # "slice" beginning to end #
>>> del l1[0]
>>> l2
[1, 2, 3, 4]
>>> l1
[2, 3, 4]
>>>
May 6 '07 #3

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

Similar topics

6
by: Peter Ballard | last post by:
Whew. I hope that title is descriptive! Hi all, The python tutorial tells me "It is not safe to modify the sequence being iterated over in the loop". But what if my list elements are mutable,...
8
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller...
3
by: yawnmoth | last post by:
I'm trying to center list elements in a webpage I'm working on, and setting margin-left to auto for ol (or ul) seems to prevent the number (or bullet) from displaying in IE6 (strict mode) and...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
1
Jezternz
by: Jezternz | last post by:
Scriptaculous needs an official forum but does not have one so I am asking here :). note you will need to be familiar with scriptaculous to help me here. Basicly I have a script that has a...
1
by: antar2 | last post by:
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper
1
by: donpro | last post by:
Hi, I have an unordered list that displays for elements horizontally. I'd like each to take up 25% of the browser window but my code does not work. Can someone help? The code is shown below. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.