0

I am trying to find an element in a list, how can I do that? This is what I wrote:

user_list = [
    {'name': 'Alizom_12',
     'gender': 'f',
     'age': 34,
     'active_day': 170},
    {'name': 'Xzt4f',
     'gender': None,
     'age': None,
     'active_day': 1152},
    {'name': 'TomZ',
     'gender': 'm',
     'age': 24,
     'active_day': 15},
    {'name': 'Zxd975',
     'gender': None,
     'age': 44,
     'active_day': 752},
] 

def find_user(user_name):
    for items in user_list:
        if user_name == 'name':
            print(user_name+" exists")
        else:
            print(user_name+" does not exists")

when I print the following I want it to find the user:

find_user('Alizom_12')
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
John
  • 11
  • 2
  • 3
    Is it not a bit of a red flag that the loop variable isn't _used_ anywhere?! You should read a basic tutorial: https://sopython.com/wiki/What_tutorial_should_I_read%3F – jonrsharpe Aug 03 '22 at 22:04
  • Wouldn't it also make more sense to write `for user in user_list:` as opposed to `items`? – martineau Aug 03 '22 at 23:22

3 Answers3

0

You're close. You need to get the value in the dictionary

if user_name == items['name']:
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

There are more than one places that your code has to be fixed:

Let's try to use the code snippet to point out them one by one.

First you can find the user_name by go through each dict. and the key name, secondly, it's better to return and not print.
Lastly, the last print should be out side of the for-loop, otherwise, you will get many repeated not found messages...


def find_user(user_name):
    for items in user_list:
        if items['name'] == user_name:
            return f'{user_name} exists. '
            #return items         # will return this user's dict. (all info)   *You have to comment prev. statement first. and uncomment this
        
    return f'{user_name} -  does not exists'


print(find_user('Alizom_12'))     #  Alizom_12 exists.
print(find_user('John Doe'))      #  John Doe -  does not exists
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
  • @John - if you have any further questions, please ask. Have ```updates``` - if user is found, it can return the ```whole dict. info```. – Daniel Hao Aug 03 '22 at 22:13
0
user_list = [
    {'name': 'Alizom_12',
     'gender': 'f',
     'age': 34,
     'active_day': 170},
    {'name': 'Xzt4f',
     'gender': None,
     'age': None,
     'active_day': 1152},
    {'name': 'TomZ',
     'gender': 'm',
     'age': 24,
     'active_day': 15},
    {'name': 'Zxd975',
     'gender': None,
     'age': 44,
     'active_day': 752},
] 

def find_user(user_name):
    userExists = False
    for items in user_list:
        for key, value in items.items():
            if key == 'name' and value == user_name:
                userExists = True
        
    if userExists is True:
        print(user_name+" exists")
    else:
        print(user_name+" does not exists")

find_user('Alizom_12')
ScriptedChicken
  • 153
  • 1
  • 6