#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    New Python user with a quick question


    Hi, I'm pretty new to Python and I'm going through a book I bought to learn. I'm learning Python so I can write scripts for ESRI products in the future.

    Anyway, going through the problems at the end of the chapter and I come across one working with a list. There were about 8 things to do to the list but they all had to be done to the original list. After seeing the solutions I think he just wanted 8 separate simple codes but I tried to combine them anyway and ran into a problem. This might not be a big deal for this program but I'd like to know why it does this for the future.

    Code:
    mylist = ["Athens", "Barcelona", "Cairo", "Florence", "Helsinki"]
    
    # A, B, C, D, E
    print "Parts A, B, C, D, E"
    print len(mylist)
    print mylist[2]
    print mylist[1:]
    print mylist[-1]
    print mylist.index("Cairo")
    print ""
    
    # F
    print "Part F"
    part_f = mylist
    print part_f
    print part_f.pop(1)
    print part_f
    print ""
    
    # G
    print "Part G"
    part_g = mylist
    print part_g
    part_g.sort(reverse = True)
    print part_g
    print ""
    
    # H
    print "Part H"
    part_h = mylist
    print part_h
    part_h.append("Berlin")
    print part_h

    My Results:

    Parts A, B, C, D, E
    5
    Cairo
    ['Barcelona', 'Cairo', 'Florence', 'Helsinki']
    Helsinki
    2

    Part F
    ['Athens', 'Barcelona', 'Cairo', 'Florence', 'Helsinki']
    Barcelona
    ['Athens', 'Cairo', 'Florence', 'Helsinki']

    Part G
    ['Athens', 'Cairo', 'Florence', 'Helsinki']
    ['Helsinki', 'Florence', 'Cairo', 'Athens']
    Part H
    ['Helsinki', 'Florence', 'Cairo', 'Athens']


    My question is: Even though I made separate variables for the parts that actually changed the list to run the code on, it appears that it changed my original list anyway?

    Is it because I said 1 equals the other so anytime I change 1 it will change the other? If this is the case, how do I set 1 to equal the other but not have them linked?

    Sorry if this is a little long, but thank you in advance
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,141
    Rep Power
    482
    Python makes assignments by reference. Use the id() function for demonstration.

    If the object being assigned is immutable (string, tuple, number, frozenset are examples) then you'll never notice the effect you've described because the object can't change. Immutable objects have a fixed hash value and can therefor be used as dictionary indexes.

    Lists and dictionaries and sets are mutable.
    Code:
    '''
       $ python /users\dlambert\temp\p.py
       44721816 44721816 # the ids are the same
       ['h', 'i', ' ', 'w', 'o', 'r', 'l', 'd']
       ['h', 'i', ' ', 'w', 'o', 'r', 'l', 'd']
    '''
    
    a = []
    b = a
    print(id(a), id(b), '# the ids are the same')
    
    a.extend('hi world')
    print(a)
    print(b)
    Copy the list for each new experiment. The shortest notation is

    part_f = mylist[:]
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Cool Deal,

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo