0

I have a dictionary with 10 elements like this

teams={'Manchester United':50, 'Liverpool': 70, 'Barcelona': 100, 'Real Madrid':90, 'Arsenal':30, 'Atletico':50}

Also I have another dictionary like this:

leagues={'England':['Manchester United', 'Liverpool', 'Arsenal'], 'Spain':['Barcelona', 'Real Madrid', 'Atletico']}

I only want to plot the graph of the English teams('Manchester United', 'Liverpool', 'Arsenal') How do I do it?

This what I have written in my code so far

import matplotlib.pyplot as plt

plt.bar(*zip(*teams.items()))
plt.show()

1 Answer 1

1

You should extract the relevant information first, one approach is:

keys_ = [k for k in teams.keys() if k in leagues['England']]
vals_ = [teams.get(k) for k in keys_]
plt.bar(keys_, vals_)
plt.show()

The first list-comprehension will extract only the keys_ that are part of the "England" league. The second will extract their corresponding values in the teams dictionary.

You can also compress it to a one-liner like:

[(k, teams[k]) for k in teams.keys() if k in leagues['England']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.