how do i get a list of all combinations for both words given?

Viewed 86

i was trying to come up with a way to get a list of words like "yes" in all possible (but common) ways like lower cased and upper case and capitalized . and then i found out you cant put two words in this function (the words "sup" & "hello") . is there a way possible to be able to have all the words in one list with this function or should start over ?

def case_insensetive(text) :
    insensetive_string = [text.lower(),text.upper(),text.capitalize()]
    print (insensetive_string)

try :
   case_insensetive("sup","hello")
except :
   raise Exception ("you screwed something")
2 Answers

This is a typical job for the map() function.

Also, use return instead of print in your function.

def case_insensetive(text):
    insensetive_string = [text.lower(),text.upper(),text.capitalize()]
    return insensetive_string


words = ['yes', 'hello']

r = list(map(case_insensetive, words))

print(r)

Output:

[['yes', 'YES', 'Yes'], ['hello', 'HELLO', 'Hello']]

If you want a single list, instead of a nested one:

flat_list = [item for sublist in r for item in sublist]
print(flat_list)

['yes', 'YES', 'Yes', 'hello', 'HELLO', 'Hello']

Just pass an arbitrary parameter(*args)..then take it can any numbers of arguments..

def case_insensetive(*texts):
    insensetive_string = []
    for text in texts:
        insensetive_string+=[text.lower(),text.upper(),text.capitalize()]
    print(insensetive_string)

case_insensetive("sup",'hello')

Output: ['sup', 'SUP', 'Sup', 'hello', 'HELLO', 'Hello']

Related