Home > front end >  Python function take list_A as input and return it as list_B
Python function take list_A as input and return it as list_B

Time:01-30

I am writing a Program that gets a list as an Input and should return one as Output. But I'm getting my original Input. The print statement print(mylist) gives me the desired output but not the return of the function

def combination_3(mylist):
    possible_words_list_3 = []

    for i in range(0,len(mylist)):
        for j in range(0,len(mylist)):
            for k in range(0,len(mylist)):
                
                new_word = mylist[i], mylist[j], mylist[k]
                possible_words_list_3.append(new_word)
    #print(possible_words_list_3)
    mylist = possible_words_list_3.copy()
    print(mylist)
    return mylist

CodePudding user response:

You are returning the input to the function. You should return your newly-created list.

def combination_3(mylist):
    possible_words_list_3 = []

    for i in range(0,len(mylist)):
        for j in range(0,len(mylist)):
            for k in range(0,len(mylist)):
                
                new_word = mylist[i], mylist[j], mylist[k]
                possible_words_list_3.append(new_word)

    return possible_words_list_3

CodePudding user response:

Thanks, I figured it out. I overwrote mylist like this:

def combination_3(mylist):
    """returns every possible word with 3 Letters """
    possible_words_list_3 = []

    for i in range(0,len(mylist)):
        for j in range(0,len(mylist)):
            for k in range(0,len(mylist)):
                
                new_word = mylist[i], mylist[j], mylist[k]
                possible_words_list_3.append(new_word)

    for i in range(0,len(possible_words_list_3)):
        mylist.append(possible_words_list_3[i])

    return mylist
  •  Tags:  
  • Related