Skip to content
Advertisement

How do I find find the index of the greatest integer in a list that contains integers and strings in Python?

Thats how the list looks like

incomes = ['Kozlowski', 52000, 'Kasprzak', 51000, 'Kowalski', 26000]

I want to print the biggest income and the surname of the person with that income (index of the income – 1)

Advertisement

Answer

If your pattern is ["SURNAME_1", INCOME_1, "SURNAME_2", INCOME_2, ... ], then you can do this:

prices = incomes[1::2] # This will return all integers
names  = incomes[::2]  # This will return all surnames

max_price = max(prices)
max_price_index = prices.index(max_price)
person = names[max_price_index]

but you should really change it to dictionary, as it is easier to work with, and it’s more efficient

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement