1

So I have this dictionary of names and values that I'm trying to print with spaces in between padded by the length of the longest key.

The code below does what I want, but the 17 is hard coded from my unit test and I'd like to replace it with the variable longest_key.

longest_key = max(len(x) for x in sorted_dict)
# longest_key is 17 for unit test
for k, v in sorted_dict:
    print("{0:<17}".format(k), v)

I tried doing what was written here: https://stackoverflow.com/a/54605046/2415706

like:

print("{0:<{longest_key}}".format(k), v)

And also using the global keyword but I keep getting a key error on longest_key. Thank you!

Update: For anyone reading this, this print mixes f-string (it's missing the f though . . . ) and .format. f-string is the newest thing and the most legible so go with that. It should just look like:

print(f"{k:<17} {v}")

Random bonus next part of my assignment, if you have to run functions on the values you can do that inside the {} like:

print(f"{some_function(k):<17} {some_other_function(v)}")
user2415706
  • 932
  • 1
  • 7
  • 19
  • 1
    Here is a line from a program I worked on. `print('{0}{1:>{2}}'.format(char, dictionary.get(char, 0), length))` and it could be reworked to do what you want. The padding is specified in `{2}` (equal to `length`) – Chris Charley Jun 04 '20 at 00:39

2 Answers2

2

You seem to be mixing styles + another minor correction for looping key values in dict

Adding multiple choices below to provide more clarity. In most of the choices, I take the liberty to assume you are using longest_key to pad as I do not see another reason for it to be computed in the code snippet

sorted_dict = {"name": "Hello", "numbers1234567890": "seventeen"}

longest_key = max(len(x) for x in sorted_dict)
# longest_key is 17 for unit test

for k, v in sorted_dict.items():
    print(f"{k:<17}", v)
for k, v in sorted_dict.items():
    print(f"{k:<{longest_key}}", v)
for k, v in sorted_dict.items():
    print(f"{k:<{longest_key}} {v}")

# for python <3.6
for k, v in sorted_dict.items():
    print("{0:<17}".format(k), v)
for k, v in sorted_dict.items():
    print("{0:<{1}}".format(k, longest_key), v)
for k, v in sorted_dict.items():
    print("{0:<{1}} {2}".format(k, longest_key, v))
for k, v in sorted_dict.items():
    print("{0:<{width}} {1}".format(k, v, width=longest_key))

I know there are other ways to do the same but I list just these as I expect them to add more clarity to the syntax of the code styles.

Aditya Guru
  • 646
  • 2
  • 10
  • 18
1

new-style string formatting was introduced in a recent version of python (3.6), and you appear to be mixing that style (f-strings) with the older style (str.format).

The f-strings style solution is what you have linked to.

If you have an older version of python and want to use str.format, I just experimented and found this solution: '{0:{1}}'.format(k, longest_key). The 0 refers to the first argument (k) and the 1 refers to the second argument, longest_key.

Kevin Wang
  • 2,673
  • 2
  • 10
  • 18
  • Oh, yeah, you're right. I didn't realize I wasn't doing f-string because I missed the f, also didn't realize was mixing styles. I'm suppose to do f-strings. I got it to work. ```print(f"{k:<{longest_key}} {v}")``` Thank you. – user2415706 Jun 04 '20 at 01:01