When you copy a list in Perl, you're duplicating the data and you end up with two distinct copies ... but when you copy a list in Python, you're copying the reference so that you end up with two names for the same variable - almost like an alias.
So in Perl - with two different copies - you end up with two sets of data that can diverge as you modify them, but in Python, if you modify the variable under one name, you're also modifying it under the other name.
If you want the Perl behaviour in Python, you can use a list slice to do the copy - replacing
tea = lunch
with
chucked = lunch[:]
but even that does only a "shallow" copy - it duplicates the references to objects within the list, so that if those objects are mutable and you change their contents, you're still chaning both.
So in my example above:
lunch[0] = "Mints"
also changes tea, but does not alter chucked whereas
lunch[2][1] = "Beans"
also changes tea
and chucked
There are source code examples here -
[source in Perl] and
[source in Python]
Original Data:
["Sausages","Mash",["Sweetcorn","Peas"],"Brocolli"]
In @tea, in Perl ... unchanged:
["Sausages","Mash",["Sweetcorn","Peas"],"Brocolli"]
In tea, in Python ... Mints and Beans have replaced Sausages and Peas:
['Mints', 'Mash', ['Sweetcorn', 'Beans'], 'Brocolli']
and in chucked, in Python ... Beans have replaced peas:
['Sausages', 'Mash', ['Sweetcorn', 'Beans'], 'Brocolli']
(written 2010-10-12, updated 2010-10-30)
Associated topics are indexed under
Y111 - Python - More on Collections and Sequences [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3150] Python dictionaries - mutable and immutable keys and values - (2011-01-29)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2894] Sorting people by their names - (2010-07-29)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[1873] List Comprehensions in Python - (2008-11-06)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[899] Python - extend v append on a list - (2006-10-20)
[633] Copying a reference, or cloning - (2006-03-05)
[386] What is a callback? - (2005-07-22)
[61] Python is a fabulous language - (2004-09-24)
Y104 - Python - Lists and Tuples [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[2719] Traffic lights in Python - (2010-04-13)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2284] Strings as collections in Python - (2009-07-12)
[2280] Creating and iterating through Python lists - (2009-07-12)
[1789] Looking for a value in a list - Python - (2008-09-08)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[955] Python collections - mutable and imutable - (2006-11-29)
[657] The ternary operator in Python - (2006-03-25)
[383] Overloading of operators on standard objects in Python - (2005-07-19)
P208 - Perl - Lists [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2295] The dog is not in trouble - (2009-07-17)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[1918] Perl Socket Programming Examples - (2008-12-02)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[968] Perl - a list or a hash? - (2006-12-06)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[773] Breaking bread - (2006-06-22)
[762] Huge data files - what happened earlier? - (2006-06-15)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[560] The fencepost problem - (2006-01-10)
[463] Splitting the difference - (2005-10-13)
[355] Context in Perl - (2005-06-22)
[240] Conventional restraints removed - (2005-03-09)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[28] Perl for breakfast - (2004-08-25)
P217 - Perl - More than Simple Lists and Hashes! [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[2877] Further more advanced Perl examples - (2010-07-19)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[293] Course follow-ups - (2005-04-27)
[43] Hash of lists in Perl - (2004-09-09)
Some other Articles
Looking forward - the next 30002999 - looking backUsing an exception to initialise a static variable in a Python function / method3D graphics - web site usage - simple matplotlib and python exampleCopying - duplicating data, or just adding a name? Perl and Python comparedA river in Melksham is not just for boaters.Python - some common questions answered in code examplesArrays v Lists - what is the difference, why use one or the otherMatplotlib - graphing in Python - teaching examplesLoading and saving data - Python / numpy