Home > Net >  Mapping two lists together
Mapping two lists together

Time:07-23

I have this line

state_names = ["Alaska", "Alabama", "Arkansas", "American Samoa", "Arizona", "California", "Colorado",
               "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
               "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts",
               "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina",
               "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York",
               "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
               "South Dakota", "Tennessee", "Texas", "Utah", "Virginia", "Vermont", "Washington",
               "Wisconsin", "West Virginia", "Wyoming"]

and i have the prices

state_price = [22.75,14.24,11.75,14.77,13.13,25.15,13.83,27.10,13.40,13.69,13.46,41.57,12.09,9.86,15.06,14.70,14.11,12.63,11.98,24.25,14.16,25.36,17.65,13.84,11.54,13.14,11.00,12.26,10.48,11.11,23.16,17.04,13.74,13.89,21.05,13.18,12.38,11.22,14.93,20.26,14.00,12.03,11.94,13.08,10.66,12.84,20.23,10.12,15.37,13.24,11.06]

i want to map each state to each price so that i can have an output to print like "The price in alaska is 22.75". So far i was able to do the state to work but cant map the prices to each of them. here is my code

user_state=input("What state are you in?: ").title()

print('The current rate in',user_state,'is',kwh,)

CodePudding user response:

First, convert the two lists into a dictionary. I did the first key-value pair to demonstrate:

state_choice = input().title()
state_names = {"Alaska": "22.75"}

print(f"The current rate in {state_choice} is {state_names.get(state_choice)}kwh.")

CodePudding user response:

It is easier to have a dict, instead of the two lists; you can use zip for that. And then you can use that dict to get information easily.

# convert into a dict
prices = dict(zip(state_names, state_price))

state = "New York"
# or, to get user input:
# state = input("Which state are you interested in? ")

print(f"The current rate in {state} is {prices[state]} kWh.")
# The current rate in New York is 21.05 kWh.
  • Related