APIs max, map, len, and f-strings in python

Let's see what is the use of the f-strings in this algorithm.  I want to print the key & value from the python dictionary as depicted below. 

J.K.Rowling 2 
Stephen King  3 
Stephenie Meyer 1 
John Green 1 
Audrey Niffenegger 1

I started with the below code, it did not give me the aligned output.

Author_Name = {"J.K.Rowling"2
               "Stephen King"3
               "Stephenie Meyer"1
               "John Green"1
               "Audrey Niffenegger"1}
for key,value in Author_Name.items():
    print(key,'\t', value)


J.K.Rowling    2 
Stephen King    3 
Stephenie Meyer    1 
John Green    1 
Audrey Niffenegger    1


From my knowledge this key length might differ, so we need to have a common key length where we can print the key string followed by value.  Let's calculate the maximum length of the key string in the given dictionary. 

len()

Author_Name = {"J.K.Rowling"2
               "Stephen King"3
               "Stephenie Meyer"1
               "John Green"1
               "Audrey Niffenegger"1}

for key,value in Author_Name.items():
    print(f"Length of Key {key} {len(key)}")



Length of Key J.K.Rowling 11
Length of Key Stephen King  12
Length of Key Stephenie Meyer 15
Length of Key John Green  10
Length of Key Audrey Niffenegger 18

max() 

Max API will return the largest item from the iterable based on the key passed.

Author_Name = {"J.K.Rowling"2
               "Stephen King"3
               "Stephenie Meyer"1
               "John Green"1
               "Audrey Niffenegger"1}

#Method-1 to calculate length of the longest Author's name 
#length = max(Author_Name, key=Author_Name.get) #Not Worked

#Method-2 to calculate length of the longest Author's name
#length = len(max(Author_Name, key=Author_Name.get)) 
#This is useful if the key is string but here the key is integer

#Method-3 to calculate length of the longest Author's name
length = len(max(Author_Name, key=len)) 

print(length)

for key,value in Author_Name.items():
    #print(key,'\t', value)

    #Method-3 to get the maximum length of Author's name
    print(f"{key:{length+1}}{value}")


18
J.K.Rowling : 2 
Stephen King  : 3 
Stephenie Meyer : 1 
John Green : 1 
Audrey Niffenegger : 1

map()

It returns a map object of the results after applying the given function to each item of a given iterable (list, tuple, etc.,) 

Author_Name = {"J.K.Rowling"2
               "Stephen King"3
               "Stephenie Meyer"1
               "John Green"1
               "Audrey Niffenegger"1}

#Method-1 to calculate length of the longest Author's name 
#length = max(Author_Name, key=Author_Name.get) #Not Worked

#Method-2 to calculate length of the longest Author's name
#length = len(max(Author_Name, key=Author_Name.get)) 
#This is useful if the key is string but here the key is integer

#Method-3 to calculate length of the longest Author's name
#length = len(max(Author_Name, key=len)) 

#Method-4 to calculate length of the longest Author's name
length = max(map(len, Author_Name.keys()))

print(length)

for key,value in Author_Name.items():
    #print(key,'\t', value)

    #Method-3 to get the maximum length of Author's name
    #print(f"{key:{length+1}}: {value}")

    print(key + ' ' * (length-len(key)),":" , value)

18
J.K.Rowling : 2 
Stephen King  : 3 
Stephenie Meyer : 1 
John Green : 1 
Audrey Niffenegger : 1

f-string

Let's see what is the purpose of the f-string firstly. 

name = "shirley"
print('Hello ' + name + ' How are you??')
print("Hello {name}, how are you??")
print(f"Hello {name}, how are you??")

F-strings are faster than the two most commonly used string formatting mechanisms, which are % formatting and str.format(). 


Comments

Post a Comment