Help With List Search

I have created a database that allows a user to create a new product entry, modify an existing one, and import and export the entire database. However, I am stuck on the search function. When I search for a specific entry by “machine” it will only print the first entry on the list that matches the criteria rather than all entries that match what the user has input. I am somewhat new to Python and this is the last component of my personal project that I need to finish it. Any and all help would be greatly appreciated.

Here is the link to my project with the text document that can be imported into the program.

Hello, @mjack360, and welcome to the forums.

def search_Machine(machine):
    findIdex = 0
    for i in buckets:
        if i[1] == machine:
            return findIdex # you are returning the first matching index only
        findIdex += 1
    return -1

Rather than returning the first matching index, you’d probably want to build a list or something with all of the matching indeces, and return that. Then you could iterate through that list, printing the elements.

Aside: In your code, you use the line import os many, many times. You only need to import it once. Typically we import necessary tools at the top of our code.

How would I go about doing that? That’s the part I am stuck on. I need it to return all the values that match the user’s input.

Regarding the import, you’re saying just put the import os at the very top and then use the os.system('cls' if == 'nt' else "printf '\033c'") where needed?

Second question first, Yes.

First question, you could do it in a similar manner to how you made your list of buckets.

def import_bkt(fname):
    temp = []
    fopen = open(fname, "r")
    for line in fopen:
        line = line.strip(' ')
        if len(line) != 0:
            line = line.split(',')
    return temp

Back to your second question, if it were me, I’d write another function called clear() or something, and just call it each time you want to clear the terminal.

def clear():
    # your code that clears the terminal

Regarding the clear command, I thought about assigning the command to a variable as I was removing all of the import os lines but figured I would wait to do all of the cleanup after the search function was resolved. I just went ahead and did it anyway though.

In reference to the search function, I still do not understand how to get it to work. I guess I just do not know enough about what I am trying to do to get it to function properly. I’ve been playing around with a few things and nothing seems to work quite like it should.

Your original search function was fine, but rather than returning the index when you have a match, append the index to a list. Something like:

stuff = [[5, 'gray', '1S4'], [3, 'red', '2M3'], [11, 'blue', '22R18'], [7, 'gray', '3G78'], [9, 'blue', '7Q49']]

def color_search(lst, color):
  result = []
  for index, item in enumerate(lst):
    if item[1] == color:
  return result

gray = color_search(stuff, 'gray')
blue = color_search(stuff, 'blue')

#print the results
print('**Gray Stuff**')
for i in gray:

print('\n**Blue Stuff**')
for i in blue:


**Gray Stuff**
[5, ‘gray’, ‘1S4’]
[7, ‘gray’, ‘3G78’]

**Blue Stuff**
[11, ‘blue’, ‘22R18’]
[9, ‘blue’, ‘7Q49’]

I’ll try and mess around with a variation of that and see what I can get going. Thank you.

1 Like

I got the search function to work by changing the function to look like this:

def search_Machine(lst, machine):
    result = []
    for index, item in enumerate(lst):
        if item[1] == machine:
    return result

And making the activation sequence look like this:

elif a == 5:
  test = input("What machine are you looking for? ")
  searchable = search_Machine(buckets, test)

  for i in searchable:

  input("Press ENTER to continue.")

Currently the output looks like this:

I need it to look like the output that shows when you “show all” on the main menu. (I tried attaching a picture of that but apparently I’m only allowed to add one.)

Any ideas on how I can make that happen? I’ve been messing with it for a while and can’t seem to get it to work quite right without messing up the search function.

I got it to work. Here is what the final print function looks like:

    elif a == 5:
        test = input("Enter machine: ")
        searchable = search_Machine(buckets, test)

        for i in searchable:
            print("Group number:", buckets[i][0])
            print("Machine:", buckets[i][1])
            print('Pin On / Coupler:', buckets[i][2])
            print('Capacity:', buckets[i][3])
            print('Width:', buckets[i][4])
            print('Profile:', buckets[i][5])
            print('Weight:', buckets[i][6])
            print('Notes & Updates:', buckets[i][7])
            print('Coupler Type:', buckets[i][8])
            print('Large Notes:', buckets[i][9])

        input("Press ENTER to return to the main menu.")


Is it possible within Python to search using a “contains” feature? Like if I had a list entry of “JRB 416”, could I search “JRB” or “416” and get it to list all of the entries?

1 Like

There is the in operator.

stuff = [["JRB 416", 123, 456], ["XYZ 123", 789, 12], ["JRB 416", 345, 678]] def get_stuff(lst, search): result = [] for s in lst: if search in s[0]: result.append(s) return result print(get_stuff(stuff, "416")) print(get_stuff(stuff, "XYZ"))

It may also be beneficial for you to investigate regular expressions.

1 Like