-2

I am new to coding and having trouble solving an issue. I found some code online regarding this subject but it did not work how I would like it to.

Let's say I have 3 abbreviated NBA teams in a list: LAL, LAC and MIL. I would, however, like the full NBA teams to be printed: Lakers, Clippers, Bucks.

Here is my code so far:

my_list=["LAL", "LAC", "MIL"]
my_dict={"HOU":"Rockets", "LAC":"Clippers", "LAL":"Lakers", "MIL": "Bucks", "BOS": "Celtics"}

for e in my_list:
    if e in my_dict:
        print(my_dict[e])
        break

Here is my current output:

Lakers

Here is my desired output:

Lakers, Clippers, Bucks

Basically I would like to find out if any of the keys in the list are present. If they are, I would like to print all of the values of the keys that are in the list.

Thanks in advance for your time and assistance. I am very grateful for any help that anyone may offer.

Able Archer
  • 579
  • 5
  • 19

1 Answers1

1

There're several issues with the code:

  • break statement forces Loop to stop, hence, there's only Lakers in the output
  • Even if there were no break statement, that output would be:
Lakers
Clippers
Bucks

For the desired output, I'd suggest the following code:

my_list=["LAL", "LAC", "MIL"]
my_dict={"HOU":"Rockets", "LAC":"Clippers", "LAL":"Lakers", "MIL": "Bucks", "BOS": "Celtics"}

l = []
for e in my_list:
    if e in my_dict:
       l.append(my_dict[e])

print(', '.join(l))
Alexander Pushkarev
  • 1,075
  • 6
  • 19
  • 2
    Note that questions with "several issues" rather than one isolated, should generally be closed rather than answered. See the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Dec 30 '19 at 22:42