Home > other >  Appending max value of a zipped list
Appending max value of a zipped list

Time:04-08

I have these three lists:

bankNames = ["bank1","bank2","bank3"]
interestRate = (0.05,0.01,0.08)
namePlusInterest = zip(interestRate,bankNames)

print(max(list(namePlusInterest)))

the print function returns an output of:

(0.08, 'bank3')

I want to be able to split the output into individual variables (for example):

MaxRate = 0.08
MaxBank = 'bank3'

So for later in my code I can say:

print(MaxBank   "has the highest interest rate of"   MaxRate)

CodePudding user response:

You can use tuple unpacking to get each individual element from the tuple:

bankNames = ["bank1", "bank2", "bank3"]
interestRate = (0.05, 0.01, 0.08)

namePlusInterest = zip(interestRate, bankNames)

MaxRate, MaxBank = max(list(namePlusInterest))

print(f"{MaxBank} has the highest interest rate of {MaxRate}")
  • Related