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. -
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
5 1661 bvdet 2,851
Expert Mod 2GB
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?
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.
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. - 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"
- 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
So in order to get the desired results I needed to create a 2D array/list which is considered a mutable container?
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. - 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
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by Edward Loper |
last post: by
|
3 posts
views
Thread by Matt Silberstein |
last post: by
|
8 posts
views
Thread by MJ |
last post: by
|
11 posts
views
Thread by Puneet |
last post: by
|
46 posts
views
Thread by junky_fellow |
last post: by
|
19 posts
views
Thread by dave |
last post: by
| |
6 posts
views
Thread by Simon Mullis |
last post: by
|
10 posts
views
Thread by Gerard flanagan |
last post: by
| | | | | | | | | | |