String inside Python list

Asked

Viewed 95 times

-1

Have string in Python stored in a variable and I want to check if it exists within a list.

I used the in for that more even with the output equal to that in the list it does not return me correctly

What I tried to:

EAN = 7896000111371
quantidade = 1
quantidade = EAN,quantidade
        print(quantidade)
        if quantidade in lista :
            print(f"quantidade confere: {quantidade}")
            print("#------------------------------#")
        else:
            print(f"Quantidade INVALIDO: {quantidade}")
            print("#------------------------------#")

Exit:

EAN encontrado: 7896000111371
('7896000111371', '1')
Quantidade INVALIDO: ('7896000111371', '1')

List:

[['7890000103404', '1'], ['7896000100634', '2'], ['7896000111371', '1'], ['7896000104335', '2'], ['7896000111364', '1'], ['7890000101059', '2'], ['7890000100724', '1'], ['7896000100870', '1'], ['7890000100472', '2'], ['7890000101943', '2'], ['7896000108869', '1'], ['7896000108852', '1'], ['7896000101679', '1'], ['7890000100816', '1'], ['7896000109040', '1'], ['7896000102324', '1'], ['7890000101219', '1'], ['7896000102768', '1'], ['7890000100687', '1'], ['7896000106322', '1']]

2 answers

2

The error happens for two reasons:

  1. As the variable 'quantity' was declared, it forms a tuple. Thus, the operator in will only return true if a tuple with elements equal to those of the variable quantity is inside the list, which is false, since the list is made up of other lists. Therefore, it is necessary to declare the variable 'quantity' as a list, using square brackets, or turn sublists into tuples;
  2. The elements of the sublists that form the list variable, because they have been declared with apostrophe ('), are treated as strings (or characters, in the case of the number). However, the elements of the quantity variable have been declared as integers. This causes the operator in, when comparing the quantity variable with the sublists of the list variable, get false as a response. Therefore, either the elements of the sublists must be declared as integers (removing the apostrophes) or the elements of the variable quantity must be declared as strings (adding the apostrophes). In the solution below, I did the second way.

The corrected code is as follows:

lista = [['7890000103404', '1'], ['7896000100634', '2'], ['7896000111371', '1'], ['7896000104335', '2'], ['7896000111364', '1'], ['7890000101059', '2'], ['7890000100724', '1'], ['7896000100870', '1'], ['7890000100472', '2'], ['7890000101943', '2'], ['7896000108869', '1'], ['7896000108852', '1'], ['7896000101679', '1'], ['7890000100816', '1'], ['7896000109040', '1'], ['7896000102324', '1'], ['7890000101219', '1'], ['7896000102768', '1'], ['7890000100687', '1'], ['7896000106322', '1']]

EAN = '7890000103404'
quantidade = '1'
quantidade = [EAN,quantidade]

print(quantidade)

for item in lista:
    print(item)
    print(quantidade)

if quantidade in lista :
   print(f"quantidade confere: {quantidade}")
   print("#------------------------------#")
else:
   print(f"Quantidade INVALIDO: {quantidade}")
   print("#------------------------------#")

 
  • 1

    Good answer =)

2


The problem is that you’re creating a tuple at the moment you do

quantidade = EAN, quantidade

However, you are comparing it to a list of lists. That is, each element in your list is another list with two elements.

In addition, the elements of the internal lists are strings and the tuple you are creating contains integer numbers, this case is easier to solve, you just need to create the list (or tuple) using the elements as strings.

You can solve the problem by creating a list instead of a tuple

lista = [['7890000103404', '1'], ['7896000111371', '1']]
quantidade = ['7896000111371', '1'] # Note o uso das aspas aqui
    
if quantidade in lista :
    print(f"quantidade confere: {quantidade}")
    print("#------------------------------#")
else:
    print(f"Quantidade INVALIDO: {quantidade}")
    print("#------------------------------#")

Or adapt the list so that the items are tuples

lista = [('7890000103404', '1'), ('7896000111371', '1')]
quantidade = '7896000111371', '1'
    
if quantidade in lista :
    print(f"quantidade confere: {quantidade}")
    print("#------------------------------#")
else:
    print(f"Quantidade INVALIDO: {quantidade}")
    print("#------------------------------#")

See working on Repl.it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.