Input dictionary key to have value be checked somewhere else

50 views Asked by At

I've got another question. You guys have been super helpful.

So I created a dictionary between NFL team names and their codes. The dictionary looks like this:

team_code = []
franchise_list = []

for row in NFL_teams:
  franchise = str(row["team_name"])
  team_id = str(row["team_id"])
  team_code.append(team_id)
  franchise_list.append(franchise)
  
nfl_dict = {}
for a,b in zip(team_code,franchise_list):
  nfl_dict.setdefault(a,[]).append(b)

This dictionary has multiple team names (values) attached to 3 letter team codes (key). For example:

  organization = input("Pick a team: ") <- LAR as input
  print(nfl_dict[organization])         <- ['Los Angeles Rams', 'St. Louis Rams', 'Los Angeles Rams'] as output

Later, I need to input a team exactly as it shows in the dataset in order to get back its record. A portion of that code looks like this:

season_year = input("Pick a year from 1966 onwards: ")
team = input("Pick a team: ")

for row in NFL_stats:
  if row["schedule_season"] == season_year:
    home = row["team_home"],row["score_home"]
    away = row["team_away"],row["score_away"]

    if team == home[0]:
      my_team = home
      other_team = away
    elif team == away[0]:
      my_team = away
      other_team = home
    else:
      continue

However, a team's name is not the same from year to year, as shown above with the LA Rams. And as it stands, if I enter a year where the team was in St. Louis as opposed to Los Angeles (for example 1999), I won't get a record back since the data states "St. Louis Rams".

I want to be able to enter the dictionary key (the three-letter code) instead of the team name. How would I be able to do so?

Thanks in advance.

1

There are 1 answers

1
Eliot Mayer On

As Samwise commented, there is a lot missing from your post, such that a full understanding is difficult. But the question you ask seems to just be, how to find something in a Python dictionary from its key. If that's the case, here is the simple answer, from the Udemy Python course I took:

# Make a dictionary with {} and : to signify a key and a value
my_dict = {'key1':'value1','key2':'value2'}

# Call values by their key
my_dict['key2']

That code returns 'value2'.

2/25/2022 update: Based on your comment, there are several ways to avoid the error you are getting. I thing you need something like along the following lines. I calculated the number of items with "len" just to show you, but I didn't even need that:

  • The "for" command just knows how many items there are.
  • If you only want the last item in a list, the [-1] index returns it without you having to know the length.
my_list =  ['Los Angeles Rams', 'St. Louis Rams', 'Los Angeles Rams']

size_of_list = len(my_list)
print ( f"my_list has {size_of_list} items, as follows:\n" )
       
for team in my_list:
    print (team)

last_team = my_list[-1]
print ( f"\nLast team on my_list: {last_team}" )

The output of that code is:

my_list has 3 items, as follows:

Los Angeles Rams
St. Louis Rams
Los Angeles Rams

Last team on my_list: Los Angeles Rams

Another approach to error messages, like you are getting, is to use a "try / except" block of code. But that is probably not as appropriate here.

If you are interested, I just learned all this stuff in a online course by Udemy. I really liked it: https://www.udemy.com/course/python-from-zero-to-hero/

Eliot