By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

Dict list question

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. user_id = ['username', 'password', 'first_name', 'last_name', 'email', 'active']
  2.  
  3. account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
  4.          'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
  5.  
  6. run = 1
  7.  
  8. while run == 1:
  9.  
  10.     user_id[1] = raw_input('Please enter a new password: ')
  11.  
  12.     if user_id[1] is account['password']:
  13.         print "Password Successfully Changed!"
  14.         run = 0
  15.  
  16.     else:
  17.         print "Password Change FAILED, please try again"
  18.  
Output:
Password Change FAILED, please try again
Dec 12 '12 #1
Share this Question
Share on Google+
5 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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?
Dec 12 '12 #2

P: 4
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.
Dec 12 '12 #3

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1. user_id = ['username', ['password'], 'first_name', 'last_name', 'email', 'active']
  2.  
  3. account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
  4.           'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
  5.  
  6. run = 1
  7. while run == 1:
  8.  
  9.      user_id[1][0] = raw_input('Please enter a new password: ')
  10.      if user_id[1] == account['password']:
  11.          print "Password Successfully Changed!"
  12.          run = 0
  13.  
  14.      else:
  15.          print "Password Change FAILED, please try again" 
Expand|Select|Wrap|Line Numbers
  1. a="abc"
  2. b="abc"
  3. print a, b, a is b
  4. c="ab"
  5. print a, c, a is c
  6. c += "c"
  7. print a, c, a is c, a == c 
Dec 12 '12 #4

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

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1. user_id = ['username', 'password', 'first_name', 'last_name', 'email', 'active']
  2.  
  3. account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
  4.           'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
  5.  
  6. print id(account['password']), id(user_id[1])  ## they are the same
  7. user_id[1]="new"
  8. print id(account['password']), id(user_id[1])  ## different
  9.  
  10. user_id = ['username', ['password'], 'first_name', 'last_name', 'email', 'active']
  11.  
  12. account = {'username':user_id[0], 'password':user_id[1], 'first_name':user_id[2],
  13.           'last_name':user_id[3], 'email':user_id[4], 'active':user_id[5]}
  14.  
  15. print "-"*30
  16. print id(account['password']), id(user_id[1])  ## they are the same
  17. user_id[1][0]="new"
  18. print id(account['password']), id(user_id[1])  ## still the same 
Dec 12 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.