how to search in List which contains tuples?

Question:

I am having a list which having tuple object and I need to search the list for all tuples which contain the string typed in search box. can anyone please help me into this?

I created one search box which having binding function. I can get the text from search box. but for the same text I need to find objects form the list
I need to append searched item in new list. so that I can update them in treeview.

from logging import root
from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("500x200")

def searchBox_fun(e):
    typed=Search_box.get()
    new_list=[]
####### I need to search here #####
    for item in data:
        if typed in item:
            print(item)
           new_list.append(item)
data=[
        ('SS Tubing, Primary Head Lower Transducer', 'ABC324232', 10.0), 
        ('Assy Tubing, L Head- Top Transducer', 'ABC093762', 20.0), 
        ('Assy Tubing, Lower Transducer-Accum Head', 'ABC099762', 13.0), 
        ('Assy Tubing ,Top Transducer- Vent Valve', 'ABC0911562', 23.0)]

Search_box=Entry(font=("Helvetica", 12), width=40)
Search_box.place(x=60, y=30)
Search_box.bind("<KeyRelease>", searchBox_fun)

root.mainloop()
Asked By: Sarjerao Shelar

||

Answers:

but for the same text I need to find objects form the list

There are many ways to search through a string (i.e. how will you handle spaces in words? If someone types "Transducer Lower" would you want the tuple in index 2 to be a result?

But to access what I think is what you’re looking for, you would index into the tuple: item[0]

So something like this:

for item in data:
    if typed in item[0]:
        ...

You can ignore case by, say, comparing the uppercase versions of typed and item[0]:

for item in data:
    if typed.upper() in item[0].upper():
        ...
Answered By: etnguyen03
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.