-2

I have a dictionary which contains a dictionary of lists:

data = {'team A': {'id': ['1', '2'], 'name': ['AAA', 'BBB']}, 'team B': {'id': ['3', '4'], 'name': ['XXX', 'YYY']}

I want to iterate over this dictionary and print values like:

|team A| 1 | AAA |
|team A| 2 | BBB |
|team B| 3 | XXX |
|team B| 4 | YYY |

How do I do this in Python?

I tried iterating over the main dictionary, but I'm struggling to print the values in the way I want it with the '|' in separate columns

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sashaank
  • 15
  • 3
  • 2
    "I tried iterating over the main dictionary" Okay; what code did you write for this? What happened when you tried that code? Could you see the values that you expected, each time through the loop? "but I'm struggling to print the values in the way I want it with the '|' in separate columns" **What is the difficulty** with this task? Do you know how to print values at all ? Do you know how to put `|` symbols in between output values? Do you know how to put multiple values on the same line? Where are you stuck? – Karl Knechtel Jan 02 '23 at 17:12
  • Also, please note that this is **not a discussion forum**. We [do not offer](https://meta.stackoverflow.com/questions/284236/) "any help" and [do not care](https://meta.stackoverflow.com/questions/343721) about your "appreciation". We provide concrete, specific answers to concrete, specific questions. – Karl Knechtel Jan 02 '23 at 17:13
  • 1
    check out "f" string formatting. https://stackoverflow.com/questions/50455784/python-and-f-strings-explanation also check out how padding in str.format() might help – JonSG Jan 02 '23 at 17:18
  • 2
    Let's say that the key 'team A' stays as it is but 'team B' becomes 'team BB' - i.e., one character longer. What should the output look like then? – DarkKnight Jan 02 '23 at 17:19
  • @KarlKnechtel its just that I'm new to data structures in python and this one is a complex one with dictionary of dictionary of lists :/ – Sashaank Jan 02 '23 at 17:31
  • Okay, but that isn't an answerable question in this format. Start by trying to think about the steps that need to be taken. Also consider using an actual discussion forum, such as Reddit or Quora. – Karl Knechtel Jan 02 '23 at 17:52
  • @Sashaank don't try and figure it all out at once. figure out how to iterate to get the first column. then add in the extra code you might need to generate the additional colum data and print at the appropriate times... – LhasaDad Jan 02 '23 at 17:55
  • @Sashaank a good start would be to get the values() of the data dict. iterate using the tuples return and proceed from there. – LhasaDad Jan 02 '23 at 17:56
  • @Sashaank give it a try and then edit your question with how far you got till you get stuck again. – LhasaDad Jan 02 '23 at 17:58

1 Answers1

-1

By using the dict.values() This will allow you to return to a list containing all the values of your dictionary, without having to specify any key.

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Japheth
  • 7
  • 3