techman
techman

Reputation: 433

Check if username exist in list and only create user if dont exist

Im validating if username or password fields are empty.

But I also want to validate if username already exist.

I have this variable userList that contains a list with all users.

And I want to show a message "Username already exist" and ask again for an username and a password if username that user enters already exist in this list.

Im trying to do this with code below but its not working.

Do you see where the issue is?

usersList Have this content:

[{u'path': u'/', u'create_date': u'2015-05-12T11:32:04', u'user_name': u'UserTest', u'user_id': u'IBM4'}, {u'path': u'/', u'create_date': u'2015-05-22T17:11:02Z', u'user_name': u'User2'}]

Code:

def createUser():
    while True:
        username = raw_input("Username:")
        password = raw_input("Password:")
        if not username or not password:
            print "Enter a valid username and password..."
        users = c.get_all_users()
        userList = users['list_users_response']['list_users_result']['users']
        if username in userList:
            print "username already exist"
        else:
            # I just want to create if fields are not empty and if username dont exist
            c.create_user(username)
createUser()

Upvotes: 0

Views: 4317

Answers (2)

Vitaly Greck
Vitaly Greck

Reputation: 668

You have list of dicts. You're trying to compare string to dict

if username in userList:

And it's not defined what get_all_users() does

Your data struct should be mapping of user name to attrs:

{
'user1': {'path': '/1', ....},
'user2': {'path': '/2', ....},
}

Upvotes: 1

abarnert
abarnert

Reputation: 365925

This would be both simpler and more efficient if you used the right data structure for the job. If you need to look up users by username, store them in a dict, with the username as a key, not in a list.

For example, if, instead of this:

userList = [{u'path': u'/', u'create_date': u'2015-05-12T11:32:04', u'user_name': u'UserTest', u'user_id': u'IBM4'}, 
            {u'path': u'/', u'create_date': u'2015-05-22T17:11:02Z', u'user_name': u'User2'}]

… you had this:

userList = {u'IBM4': {u'path': u'/', u'create_date': u'2015-05-12T11:32:04', u'user_name': u'UserTest', u'user_id': u'IBM4'}, 
            u'User2': {u'path': u'/', u'create_date': u'2015-05-22T17:11:02Z', u'user_name': u'User2'}}

… then your existing code would work:

if username in userList:

But if you really want to use your existing data structure (which, again, you shouldn't), the problem is that username in userList is asking for whether any of the things in userList are equal to username. But the things in userList aren't usernames, they're dictionaries, so none of them are going to be equal.

What you need to do is:

if any(user['user_name'] == username for user in userList):

Notice that this is a lot more complicated. It's also a lot slower if you have more than a handful of users, because it has to go through every user one by one and check its user_name against username. That's why you want to use a dict rather than a list.

Upvotes: 1

Related Questions