Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Copying - duplicating data, or just adding a name? Perl and Python compared

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 as below, or enter http://melksh.am/nnnn for individual articles
P208 - Perl - Lists
  [4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
  [3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [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)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [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)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [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)

Y104 - Python - Lists and Tuples
  [4722] Embedding more complex code into a named block - (2016-11-04)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [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)
  [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)
  [899] Python - extend v append on a list - (2006-10-20)
  [657] The ternary operator in Python - (2006-03-25)
  [383] Overloading of operators on standard objects in Python - (2005-07-19)

Y111 - Python - More on Collections and Sequences
  [4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
  [3797] zip in Python - (2012-07-05)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [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)
  [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)


Back to
A river in Melksham is not just for boaters.
Previous and next
or
Horse's mouth home
Forward to
3D graphics - web site usage - simple matplotlib and python example
Some other Articles
Looking forward - the next 3000
2999 - looking back
Using an exception to initialise a static variable in a Python function / method
3D graphics - web site usage - simple matplotlib and python example
Copying - duplicating data, or just adding a name? Perl and Python compared
A river in Melksham is not just for boaters.
Python - some common questions answered in code examples
Arrays v Lists - what is the difference, why use one or the other
Matplotlib - graphing in Python - teaching examples
Loading and saving data - Python / numpy
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2996_Cop ... pared.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat