'Delete several TwoLineListItem from an MDList when creating a dinamic list without an ids in Kivy

Help here pls, I got to create several TwoLineListItem in an dynamic MDList,This start working when the list (list_general) have an item and this is gotten from the FirstWindow(Screen), This part is working so far, and now I want to get delete them when clicking in one of these ones (TwoLineListItem) . In my py.file, inside my def imprimir(), I assigned an id and a function "remove_widget" in the on_press= , which are in the variable "items". But It's not working I think I'm doing something wrong and I got this error too.

AttributeError: 'str' object has no attribute 'unbind'

The problem is exactly in the id= or the on_press=

py.main

class SecondWindow(Screen):
    list_general= []
    list_price= []
    md= ObjectProperty(None) #I need the MDList el id of container USar ObjectProperty()
    def imprimir(self, list_general, list_price):    
        print(list_general)
        for i in range(len(list_general)):
            #print(i)
            items= TwoLineListItem(text= list_general[i],secondary_text= "$"+list_price[i] + " Dollars" , id= 'item'+str(i),on_press= self.remove_widget('item'+str(i)))
            self.md.add_widget(items) # It'S Working now
            list_general.pop(i)
            list_price.pop(i)

kv.main

        
<SecondWindow>:
    name: "Buy"
    md: container1
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            text: "Productos Añadidos"
            font_size: 32
    
        
        ScrollView:
            MDList:
                
                id: container1
                #OneLineListItem: 
               

        Button:
            text:"press"
            


Solution 1:[1]

From what i understand, this should solve your issue. correct me if i'm wrong about the solution provided

class SecondWindow(Screen):
    list_general= []
    list_price= []
    md = ObjectProperty(None)

    def imprimir(self, list_general, list_price):    
        print(list_general)
        for i in range(len(list_general)):
            #print(i)
            list_item = TwoLineListItem(
                text = list_general[i],
                secondary_text = "$" + list_price[i] + " Dollars"
            )
            list_item.bind(on_release = lambda x: self.remove_widget_instance(list_item, self.md))
            self.md.add_widget(list_item)
            list_general.pop(i)
            list_price.pop(i)

    def remove_widget_instance(self, instance, parent_widget):
        parent_widget.remove_widget(instance)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 darkmatter08