CrazyCoder
CrazyCoder

Reputation: 517

Python: Printing a single element as a string and printing a list when there are multiple elements in list

I have the following dictionary (where the counts (values) are sorted in reverse):

sorted_dict={'A': 4, 'W': 4, 'T': 2, 'S': 2, 'I': 2, 'R': 1}

As you can see there are 2 keys with the same values ie. A and W.

I have written the the follow logic to get the max count

max_count = list(sorted_dict.values())[0]
max_count_letter_list = []

After iterating through each key in the dictionary, I add the letters with max counts to the list.

for letter in sorted_dict:
    if sorted_dict[letter] == max_count:
        max_count_letter_list.append(letter) 

When printing to the console, it is printed as a list which is what I expect

print("Most frequent letter \"{}\" appears {} times"
          .format(max_count_letter_list, max_count))

Output: Most frequent letter "['W', 'A']" appears 4 times

However, if I have a single element in the list, it prints out like this:

Most frequent letter "['A']" appears 4 times

My expectation is to print like this: Most frequent letter "A" appears 4 times

Question: In the one line print statement, how do I print just a single element from the list without the brackets and if it is a list, it should print the list of elements. Do I need to write a if statement or is there a better way to write this?

Upvotes: 0

Views: 220

Answers (3)

CryptoFool
CryptoFool

Reputation: 23089

I think this is what you were thinking about...a conditional that prints differently based on how many items are in the list:

max_count_letter_list = ['A', 'W']
max_count = 4
print("Most frequent letter \"{}\" appears {} times".format(max_count_letter_list[0] if len(max_count_letter_list) == 1 else max_count_letter_list, max_count))
max_count_letter_list = ['A']
print("Most frequent letter \"{}\" appears {} times".format(max_count_letter_list[0] if len(max_count_letter_list) == 1 else max_count_letter_list, max_count))

Result:

Most frequent letter "['A', 'W']" appears 4 times
Most frequent letter "A" appears 4 times

Upvotes: 2

Nithin Gowda
Nithin Gowda

Reputation: 131

print("Most frequent letter \"{}\" appears {} times"
.format((max_count_letter_list[0] if len(max_count_letter_list) ==1 else max_count_letter_list), 
max_count)

I think this is what you are looking for.

Upvotes: 0

azro
azro

Reputation: 54148

A solution that fits for all could be to join items with a comma

print("Most frequent letter \"{}\" appears {} times".format(",".join(max_count_letter_list), max_count))

# Giving
Most frequent letter "A,W" appears 4 times
Most frequent letter "A" appears 4 times

If you want to handle them differently you need a condition to separate the cases : an if , then just write it normally or use the inline mode

 # inline
print("Most frequent letter \"{}\" appears {} times".format(letter_list[0] if len(letter_list) == 1 else letter_list, max_count))

# multi-line
if len(letter_list) == 1:
    print("Most frequent letter \"{}\" appears {} times".format(letter_list[0], max_count))
else:
    print("Most frequent letter \"{}\" appears {} times".format(letter_list, max_count))

Solution to shorten the before code

from itertools import groupby
from operator import itemgetter
g = groupby(sorted_dict.items(), key=itemgetter(1))
max_count, letter_list = next((count, [x[0] for x in vals]) for count, vals in g)

Upvotes: 3

Related Questions