Dict list question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Layvian
    New Member
    • Dec 2012
    • 4

    Dict list question

    I was attempting to change a value within a dictionary list via a list variable. However it does not change. Is this a feature of a Dictionary list or am I doing something incorrect? Thank you in advance for any assistance.

    Code:
    user_id = ['username', 'password', 'first_name', 'last_name', 'email', 'active']
    
    account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
    		 'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
    
    run = 1
    
    while run == 1:
    
    	user_id[1] = raw_input('Please enter a new password: ')
    	
    	if user_id[1] is account['password']:
    		print "Password Successfully Changed!"
    		run = 0
    		
    	else:
    		print "Password Change FAILED, please try again"
    Output:
    Password Change FAILED, please try again
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    When the assignment to account is made, user_id[1] is evaluated as a string and is not a reference to user_id. Why do you need the list user_id anyway?

    Comment

    • Layvian
      New Member
      • Dec 2012
      • 4

      #3
      I really do not have to use it. Really just curious more then anything. I noticed when I added a print syntax it showed me the correct value. I figured if anything it had to be how the value was stored. Thank you for your assistance.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Strings are immutable so you would have to use a container that is mutable, like a list, to have the dictionary point to the same memory location (otherwise the dictionary stays with the original entry). Also "is" is different from "==", but would work in my code (not in yours) because they both point to the same object.
        Code:
        user_id = ['username', ['password'], 'first_name', 'last_name', 'email', 'active']
         
        account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
                  'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
         
        run = 1
        while run == 1:
         
             user_id[1][0] = raw_input('Please enter a new password: ')
             if user_id[1] == account['password']:
                 print "Password Successfully Changed!"
                 run = 0
         
             else:
                 print "Password Change FAILED, please try again"
        Code:
        a="abc"
        b="abc"
        print a, b, a is b
        c="ab"
        print a, c, a is c
        c += "c"
        print a, c, a is c, a == c

        Comment

        • Layvian
          New Member
          • Dec 2012
          • 4

          #5
          So in order to get the desired results I needed to create a 2D array/list which is considered a mutable container?

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            The mutable container has to be whatever you want to link, so both reflect the change. So if you want to change a certain element of a list, then that element has to be a sub-list and not a string, which is different from a 2D array as the rest of the elements are still strings.
            Code:
            user_id = ['username', 'password', 'first_name', 'last_name', 'email', 'active']
             
            account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
                      'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
             
            print id(account['password']), id(user_id[1])  ## they are the same
            user_id[1]="new"
            print id(account['password']), id(user_id[1])  ## different
            
            user_id = ['username', ['password'], 'first_name', 'last_name', 'email', 'active']
             
            account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
                      'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
            
            print "-"*30
            print id(account['password']), id(user_id[1])  ## they are the same
            user_id[1][0]="new"
            print id(account['password']), id(user_id[1])  ## still the same

            Comment

            Working...