473,406 Members | 2,769 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,406 software developers and data experts.

Mutability, copying lists but not sharing?

cnb
Is it not possible to have mutability without this? I know I can use
sorted and list(reversed) instead of .sort and .reverse but if I want
to copy a list and then change that list without changing the first
one?
And there isn't a .copy function so I have to "new = [] for element in
list: new.append(element)" ?
(I guess mutability is there for performance? Because I prefer a =
sorted(a) conceptually.)
>>a = [1,2,3]
b = a
b.append(4)
b
[1, 2, 3, 4]
>>a
[1, 2, 3, 4]
>>c = "hello"
d = c
d += " sir!"
c
'hello'
>>d
'hello sir!'
>>>


and what is the difference between extend and + on lists?
Aug 26 '08 #1
1 869
cnb wrote:
And there isn't a .copy function so I have to "new = []
for element in list: new.append(element)"?
You can do
new = list(old)
which I like for being explicit, or just
new = old[:]
and what is the difference between extend and + on lists?
>>a = range(3)
b = a + range(3)
b
[0, 1, 2, 0, 1, 2]
>>a
[0, 1, 2]
>>a.extend(range(3))
a
[0, 1, 2, 0, 1, 2]

hth,
Alan Isaac
Aug 26 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
11
by: hana1 | last post by:
Hello guys, I just have a quick question about C++. I am moving from Java to C++. And as many of Java users know, all linked list and trees are already implemented and you just have to instantiate...
22
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
0
by: postings | last post by:
Hi I'm a little confused. I've deployed my ASP.NET project by using the "copy project" feature of VS2003. Works fine. I'm stuck however in copying individual files to the deployed solution. ...
17
by: ex_ottoyuhr | last post by:
I'm trying to create a function that can take arguments, say, foo and bar, and modify the original copies of foo and bar as well as its local versions -- the equivalent of C++ funct(&foo, &bar). ...
8
by: daD | last post by:
I'm trying to write a small database that tracks people coming and going from a small campground. I need to have the current guests in the "current" table" and then have the ability to check them...
1
by: Claire | last post by:
Im writing a small application to allow some none techy people create localized string resources for our main application. So we can reuse modules each one has its own .resx resource file. So my...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.