Ulrik Aase
Ulrik Aase

Reputation: 13

How can I print from a dictionary?

I am wondering how I can print all keys in a dictionary on one line and values on the next so they line up.

The task is to create a solitaire card game in python. Made most of it already but I wish to improve on the visual. I know how to use a for loop to print lines for each value and key, but the task I'm doing in school asks me to do it this way. I also just tried to create new lists for each line and "print(list1)" print(list2) but that just looks ugly.

    FireKort ={
        'A': None,#in my code i have the cards as objects here with value 
    #and type
        'B': None,#ex. object1: 8, cloves; object2: King, hearts 
        'C': None,
        'D': None,
        'E': None,
        'F': None,
        'G': None,
        'H': None
        }
    def f_printK():
        global FireKort
        for key in FireKort:
            print('Stokk:',key,' Gjenstående:',len(FireKort[key]))
            try:
                print(FireKort[key][0].sort, FireKort[key][0].valør)
            except:
                print('tom')


    ##here are the lists i tried:
    ##    navn=[]
    ##    kort=[]
    ##    antall=[]
    ##    for key in FireKort:
    ##        navn.append((key+' '))
    ##        kort.append([FireKort[key][0].sort,FireKort[key][0].valør])
    ##        antall.append(  str(len(FireKort[key])))
    ##    print(navn)
    ##    print(kort)
    ##    print(antall)

A B C D E F G H [♦9][♣A][♠Q][♣8][♦8][♣J][♣10][♦7] 4 4 4 4 4 4 4 4

Upvotes: 0

Views: 256

Answers (3)

Daweo
Daweo

Reputation: 36450

It could be done using ljust method of str. Example:

d = {'A':'some','B':'words','C':'of','D':'different','E':'length'}
keys = list(d.keys())
values = list(d.values())
longest = max([len(i) for i in keys]+[len(i) for i in values])
print(*[i.ljust(longest) for i in keys])
print(*[i.ljust(longest) for i in values])

Output:

A         B         C         D         E        
some      words     of        different length   

Note that I harnessed fact that .keys() and .values() return key and values in same order, if no action was undertaken between them regarding given dict.

Upvotes: 0

Radosław Cybulski
Radosław Cybulski

Reputation: 2992

Try this:

d = { ... }
keys = [ str(q) for q in d.keys() ]
values = [ str(q) for q in d.values() ]
txts = [ (str(a), str(b)) for a, b in zip(keys, values) ]
sizes = [ max(len(a), len(b)) for a, b in txts ]
formats = [ '%%%ds' % q for q in sizes ]
print(' '.join(a % b for a, b in zip (formats, keys)))
print(' '.join(a % b for a, b in zip (formats, values)))

In short:

  • first we get str values of keys and values of dictionary d (since we're going to use them twice, we might as well store them locally)
  • we calculate max size of each "column"
  • we create formats for % operator
  • and we print

Upvotes: 0

Michael
Michael

Reputation: 1203

Have you try to use pprint?

The pprint module provides a capability to “pretty-print arbitrary Python data structures

https://docs.python.org/2/library/pprint.html

Upvotes: 1

Related Questions