Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python for loops - applying a temporary second name to the same object

If you write a for loop in Python to go through each element of a list, you assign an extra (temporary) name to each member of the list while within the loop. So than any changes made to the named variable will also be changes to the object in the original list. However, that's overshaddowed / overtaken by the fact that changes to immutable objects result in the creation of new objects which the temporary name, but not the original name, point to. So that changes made to IMMUTABLE objects will NOT be seen as changes to objects in the original list.

Int and float primitives, and tuples, are IMMUTABLE.
Lists and dicts are MUTABLE, as are most other objects.

Here's an example showing how the behaviour of lists and tuples differs in this respect - even though both are zero based ordered collections of objects:

  # for loop changes mutable objects in list
 
  stuff = [3,4,[5,6],7,8]
  for item in stuff:
     item *= 2
  print stuff
 
  # for loop leaves immutable objects in tuple
 
  stuff = [3,4,(5,6),7,8]
  for item in stuff:
     item *= 2
  print stuff


And here is how that runs:

  munchkin:ps grahamellis$ python over
  [3, 4, [5, 6, 5, 6], 7, 8]
  [3, 4, (5, 6), 7, 8]
  munchkin:ps grahamellis$ """
(written 2011-09-14)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y103 - Python - Conditionals and Loops
  [4210] If elif elif elif - multiway selection in Python - (2013-11-16)
  [4092] Identity in Python - (2013-05-17)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3762] Learning to program - the if statement. Python. - (2012-06-12)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1661] Equality, sameness and identity - Python - (2008-05-31)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1201] No switch in Python - (2007-05-23)
  [909] Python is like a narrowboat - (2006-10-30)
  [835] Python - when to use the in operator - (2006-08-16)
  [788] New - Conditional expressions in Python 2.5 - (2006-07-01)
  [668] Python - block insets help with documentation - (2006-04-04)
  [657] The ternary operator in Python - (2006-03-25)
  [353] Wimbledon Neck - (2005-06-20)
  [299] What - no switch or case statement? - (2005-05-03)

Y111 - Python - More on Collections and Sequences
  [3797] zip in Python - (2012-07-05)
  [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)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [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)


Back to
Melksham to become a part of Trowbridge?
Previous and next
or
Horse's mouth home
Forward to
Research is exciting. But should routine be automated?
Some other Articles
Getting more log information from the Apache http web server
A demonstration of how many Python facilities work together
Pressing ^C in a Python program. Also Progress Bar.
Research is exciting. But should routine be automated?
Python for loops - applying a temporary second name to the same object
Melksham to become a part of Trowbridge?
Light bulbs
Moving from scripting to Object Orientation in Python
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby
Sundays - and over eating
4298 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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3439_Pyt ... bject.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat