How to print the name of each dictionary?

Question:

I have just started coding this semester, so if you can use simple methods to help me find my answer I’d appreciate it. Basically, I just want it to print the name of each dictionary and then list it’s contents. Oh, and just so you know, I don’t actually even like sports this was just a previous homework assignment that I wanted to improve upon. Here’s what I’ve got and yes, I know it doesn’t work the way I want it to:

football = {
            'favorite player': 'Troy Aikman',
            'team': 'Dallas Cowboys',
            'number': '8',
            'position': 'quarterback'
           }
baseball = {
            'favorite player': 'Jackie Robinson',
            'team': 'Brooklyn Dodgers',
            'number': '42',
            'position': 'second baseman'
           }
hockey = {
          'favorite player': 'Wayne Gretzky',
          'team': 'New York Rangers',
          'number': '99',
          'position': 'center'
         }

sports = [football, baseball, hockey]
my_sports = ['Football', 'Baseball', 'Hockey']
for my_sport in my_sports:
    print(my_sport)
for sport in sports:
    for question, answer in sport.items():
        print(question.title + ": " + answer)
    print("n")

I want it to print:

Football
Favorite player: Troy Aikman
Team: Dallas Cowboys
Number: 8
Position: quarterback

Baseball:
Favorite player: Jackie Robinson
Team: Brooklyn Dodgers
Number: 42
Position: second baseman

…and so forth. How do I achieve the results I want? The simpler the better and please use Python 3, I know nothing of Python 2.

Asked By: Ginger Hale

||

Answers:

UPDATED:

I edit my answer and now the code below works:

my_sports = {'Football': football, 'Baseball' : baseball, 'Hockey' : hockey}

for key,value in my_sports.items():
    print(key)

    for question, answer in value.items():
        print(question + ": " + answer)
    print("n")

This is the result:

Football
Favorite Player: Troy Aikman
Team: Dallas Cowboys
Number: 8
Position: quarterback


Baseball
Favorite Player: Jackie Robinson
Team: Brooklyn Dodgers
Number: 42
Position: second baseman


Hockey
Favorite Player: Wayne Gretzky
Team: New York Rangers
Number: 99
Position: center

Code here:
https://repl.it/MOBO/3

Answered By: Kenji Mukai

You can try this:

sports = {"football":football, "baseball":baseball, "hockey":hockey}
for a, b in sports.items():
    print(a)
    for c, d in b.items():
        print("{}: {}".format(c, d))

Output:

football
position: quarterback
favorite player: Troy Aikman
number: 8
team: Dallas Cowboys
baseball
position: second baseman
favorite player: Jackie Robinson
number: 42
team: Brooklyn Dodgers
hockey
position: center
favorite player: Wayne Gretzky
number: 99
team: New York Rangers
Answered By: Ajax1234
my_sports = {'Football': football, 'Baseball' : baseball, 'Hockey' : hockey}
for key,value in my_sports.items():
    print(key)

    for question, answer in value.items():
        print(question + ": " + answer)
    print("n")
Answered By: Dharmesh Fumakiya

The built-in zip function seems like the easiest way to combine and pair-up elements from the two lists. Here’s how to use it:

sports = [football, baseball, hockey]
my_sports = ['Football', 'Baseball', 'Hockey']

for my_sport, sport in zip(my_sports, sports):
    print('n'.join((
        '{}',  # name of sport
        'Favorite player: {favorite player}',
        'Team: {team}',
        'Number: {number}',
        'Position: {position}')).format(my_sport, **sport) + 'n'
    )
Answered By: martineau
Dictionary={
            "Name":"Ali",
             "Roll":1,
             "Name 1":"asad"
             " Roll 1":2,
             }
Print (Dictionary)
Answered By: Shabnam Kaneez
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.