0

I have this dictionary:

 self.biography = {'Name' : '', 'Age' : 0, 'Nationality' : '', 'Position' : '', 'Footed' : ''}

I have this method

self.bio_list = []

    def create_player(self):
        for key in self.biography.items():
            value = input(key + ': ')
            temp = [key, value]
            self.bio_list.append(temp)
        print(self.bio_list)
        for i in self.bio_list:
            print(i)

I want a way to print out it like so:

Name: Richard

Age: 23

Nationality: British

etc, without the nasty () and also I want the program to print them out the same way they are listed in the dictionary, currently it will just print out in any random order.

NewUser123
  • 35
  • 6
  • 2
    Dictionaries are **unordered**. The output order you see is [not random](https://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary) however. – Martijn Pieters May 09 '16 at 12:56
  • 2
    Looks like you might want to be using a namedtuple for this instead of a dictionary, possibly. – miradulo May 09 '16 at 12:57
  • Looping and printing is as simple as `for key, value in yourdict.items(): print(key, value, sep=': ')` – Martijn Pieters May 09 '16 at 12:59
  • @DonkeyKong: indeed, and then loop over `tup._asdict().items()` to loop over the attributes and their values in tuple order. – Martijn Pieters May 09 '16 at 13:00
  • @MartijnPieters Exactly what I had in mind, just typed it out out of boredom, much nicer I think :) – miradulo May 09 '16 at 13:03
  • @MartijnPieters your 2nd comment still prints them out in anyway order right? – NewUser123 May 09 '16 at 13:14
  • @NewUser123: the `namedtuple._asdict()` method returns an OrderedDict object, which is created with the correct order for the namedtuple attributes. For my `yourdict.items()` look, if `yourdict` is an OrderedDict object, the order will be determined by the insertion order. – Martijn Pieters May 09 '16 at 13:17
  • dict object has no attribute _asdict. – NewUser123 May 09 '16 at 13:24
  • @NewUser123 Didn't you question calling `_asdict()` on a dictionary? Did you read the part about using a `namedtuple` instead? – miradulo May 09 '16 at 13:33

0 Answers0