List in python does not show index

Question:

Dueno = [28957346, 'Juan', 'Perez', 4789689, 'Belgrano 101']
dni = 26000000
print('Su dni es: ')
input()
for elementos in Dueno:
    if dni > 26000000:
        print(Dueno[3])
    else:
        break

I’m trying to show the owner’s phone if the ID is greater than 26000000 / but I can’t find that, help.

Asked By: Noobuyer

||

Answers:

  1. Your dni variable is not greater than 26000000, so the condition won’t pass.
  2. You don’t need a for loop here, like at all, its code does nothing with the loop itself.
Answered By: Daniel

I’m assuming that you will have multiple owners (Dueno) and you want to grab the fourth number in the list (which is their phone number) if their ID (the first number in the list) is greater than 26000000.

So for that, you would want to use a for loop:

Duenos = [[28957346, 'Juan', 'Perez', 4789689, 'Belgrano 101'], 
         [14952151, 'Pedro', 'Lopez', 5214649, 'Belgrano 102'],
         [31152151, 'Carlos', 'Garcia', 1214589, 'Belgrano 103'],]
dni = 26000000

numeros = []
for Dueno in Duenos:
    if Dueno[0] > dni:
        numeros.append(Dueno[3])
print(numeros)

[4789689, 1214589] # Output

Or you could use list comprehension:

numeros = [Dueno[3] for Dueno in Duenos if Dueno[0] > dni]
print(numeros)
[4789689, 1214589] # Output
Answered By: Michael S.
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.