Search for element in list of list Python
<p>New to programming.
I'm trying to create a program that has a list o list (As a data base) the first element being an invoice, the second the value of that invoice and the third, the margin of income. Given an invoice (input), it will search within the list o lists(data base).</p>

<p>I currently have:</p>

<pre><code>Data_base= [["f1",2000,.24],["f2",150000,.32],["f3",345000,.32]]
invoice = input("Enter invoice: ")

if invoice in data_base :
    print ("Invoice is valid")

else :
    print("Invoice does not exist, enter a valid document: ")
</code></pre>

<p>When I run the program and I enter a valid element within the list, it does not recognize as an element within the list.</p>

<p>What am I doing wrong?</p>

<p>What is the code to be given the index if the element is within the list of lists?</p>

<p>If you wish to search by invoice number, a better data structure to use is the dictionary. Use the invoice number as the key, and the invoice value and margin as values. Example:</p>

<pre><code>invoices = {'f1': [2000, .24],
            'f2': [150000, .32],
            'f3': [345000, .32]}

invoice = input("Enter invoice: ")
if invoice in invoices:
    value, margin = invoices[invoice]
    print('Invoice {} is valid. Value {}, margin {}'.format(invoice, value, margin))
else:
    print("Invoice does not exist, enter a valid document: ")
</code></pre>

<p>Now when you say <code>invoice in invoices</code> Python will search for the invoice number in the dictionary's keys. Compared to searching in a list this is a very efficient operation (O(1) vs. O(n)).</p>

python list search element