Home > Back-end >  Return element from list matching first character pattern
Return element from list matching first character pattern

Time:10-22

I have the following list

students = ['44560 Susan Pierce 46', '49509 John Lee 74', '49510 Kim Note 76', '49519 Jennifer Six 50', '49556 Maria Cruz 67', '49586 Martin Lewis 79', '55900 Richard White 100']

I created this function which is supposed to return the entire element of the list if the inputted number matches the first five characters:

def search_student(number, students): 
    
    student = [] 
    for i in students: 
        student.append(i[0:5]) 
        
    for i in range(0, len(students)): 
        if student[i] == number: 
            return students[i]
        else:
            return "student not found"

As for now my code only works for the first element but not for any of the other ones.

Any suggestions why the second for loop does not iterate through the entire list?

Thankful for any suggestions or error detections.

CodePudding user response:

This is because using return will immediately break the loop in the first iteration. You need to temporarily save all the results and then return your results after the loop is finished. You could do this as follows:

students = ['44560 Susan Pierce 46', '49509 John Lee 74', '49510 Kim Note 76', '49519 Jennifer Six 50', '49556 Maria Cruz 67', '49586 Martin Lewis 79', '55900 Richard White 100']


def search_student(number, students): 
    
    student = [] 
    for i in students: 
        student.append(i[0:5]) 
        
    result = []
    for i in range(0, len(students)): 
        if student[i] == number: 
            result.append(students[i])
    
    if len(result) == 0:
        print('Student not found')
    else:
        return result
#Run it
print(search_student('49509', students))
#Output: ['49509 John Lee 74'] 

Edit: This solution uses a list for the result. If the number you are using to identify the entries in your students list is unique, you can just use a string instead. Using a list makes sense if multiple results are possible.

CodePudding user response:

I recommend to slightly restructure your code:

1.) create students dictionary from the list where keys will be student's numbers 2.) search_student() function then won't iterate over all items in dictionary but just check for the key

students = [
    "44560 Susan Pierce 46",
    "49509 John Lee 74",
    "49510 Kim Note 76",
    "49519 Jennifer Six 50",
    "49556 Maria Cruz 67",
    "49586 Martin Lewis 79",
    "55900 Richard White 100",
]

# convert the students list to dictionary
# key = number
# value = student

students_dct = {}
for s in students:
    id_, rest = s.split(maxsplit=1)
    students_dct[id_] = rest


def search_student(number, students):
    if number in students:
        return students[number]

    return "Student {} not found".format(number)


print(search_student("49509", students_dct))

Prints:

John Lee 74

EDIT: If you want to use a list as an argument of the function:

students = [
    "44560 Susan Pierce 46",
    "49509 John Lee 74",
    "49510 Kim Note 76",
    "49519 Jennifer Six 50",
    "49556 Maria Cruz 67",
    "49586 Martin Lewis 79",
    "55900 Richard White 100",
]


def search_student(number, students):
    for s in students:
        if s.startswith(number):
            return s

    return "Student {} not found".format(number)


print(search_student("49509", students))

Prints:

49509 John Lee 74
  • Related