Why do I get these results in my terminal?

Why when i run the same piece of code in a function and also outside a function do I get different results.

  1. When code is inside function it returns False
  2. When code is outside function it returns False, none, False, none, none, False, none, False. I was hoping it would .append my word to the empty list i provided.

*check code below (I am writing it via jupyter notebook)

words_list = ['palpatate', 'neuron', 'DNA', 'synapse', 'vein', 'labotomy', 'nerve', 'cerebellum']

empty_list_list = [[] for i in words_list]

def find_word_index(word):
    word_index = words_list.index(word)
    return word_index
#this function works

def add_word_to_list(word1, word2):
    word_index = find_word_index(word1)
    word_for_list = empty_list_list[word_index].append(word2)
#this function works
    
add_word_to_list('neuron', 'neuron')
add_word_to_list('palpatate', 'palpatate')
add_word_to_list('DNA', 'DNA')
add_word_to_list('synapse', 'synapse')
add_word_to_list('vein', 'vein')
add_word_to_list('labotomy', 'labotomy')
add_word_to_list('nerve', 'nerve')
add_word_to_list('cerebellum', 'cerebellum')


#Function form of checking if character is in string using "in" conditional with "for loops" to iterate through list
def find_word(list, letter):
    for data in list:
        for ele in data:
            if letter in ele:
                return True
            else:
                return False
    

print(find_word(empty_list_list,'n'))

#Non-Function form of checking if character is in string using "in" conditional with "for loops" to iterate through list
word = 'n'
common_letters = []
for ele in empty_list_list:
    for data in ele:
        if word in data:
            print(common_letters.append(word))
        else:
            print(False)

Please post your code in Python, a language in which indentations are a fundamental part of the code, not something to be left up to guesswork.

If you do so, your readers will be easily able to parse your code, and, if desired, copy and paste it for testing.

You can preserve the indentations by making use of the </> icon that appears in the menu bar atop the text box when you open it to type.

1 Like

I’m confused by what you mean, “Please post your code in Python…”. I thought I had posted my code in Python. Also, there is indentation showing (wether it is right or wrong I’m not sure).

1 Like

I apologize; when I first saw your post, there was no code box, and no indentations. Perhaps my browser did not load it correctly.

The return statement does three things:

  1. It evaluates the expression to the right of the word, return to retrieve a value
  2. It passes that value to the calling statement or function.
  3. It halts processing of the function. When return is reached one time, the function halts, and all of its local variables are lost from the stack (memory region containing “active” statements and functions), as is the function itself, until it is called again.

Now, in your very first example, this works perfectly. The function find_word_index() is repeatedly called for one index at a time. It dutifully returns one index at a time to the calling statement word_index = find_word_index(word1) within the function add_word_to_list(), then vanishes from the mind of the Python interpreter until called again.


However, in find_word() (assuming that the parameter list is a list of lists of strings, as in the first example), if the first string in the first list within list contains the character letter, the function will return True and halt. If it does not contain that character, the function will return False and halt.

In other words, yes, as you observed, the function returns True or False one time only, due to the nature of return.


The final example uses the print() function, rather than return (since return cannot be used outside of a function). Because print() does not halt processing, it does what you expect.


Remember:

  • We use print() when we want to see data printed to the screen
  • We use return when Python needs to use the data, to pass to another function, for instance, or to a calling statement. (Exactly as you showed in your first example.)
1 Like

Very helpful. Thank you patrickd.
The only unanswered question that I can’t seem to figure out is why the final example I use
I assume Python has processed that it is not False, so then the other option it has is to .append the word (in this case “n”, which is in some of those strings) to the empty list “common_letters” and print that, but it is printing “none” instead…

2 Likes

Are you trying to append to a list or print something? Pick one.

1 Like

I’m trying to print(common_letters.append(word)). Is that something that can’t be done in Python. Do I need to first .append and then print on a separate line?

1 Like

You’re printing the return value of list.append.
What is the behaviour of list.append? What does it take as input, does it have any side effects, what does it return? And, is that return value something that you were looking to print?

So, not a python limitation, no, but a matter of … fitting the pieces together.

In some languages you’ve got no side effects so appending necessarily results in a new list. Others might have the side effect of changing it, and among those they might or might not return something, and what they return might be the appended value, or the list. But the caller already has access to those so there’s not too much of a point to that.

You could argue it would be nice to be able to “continue to use” that expression, maybe something like:

mylist.append(5).append(3)

But I wouldn’t be too happy about writing it like an expression when really they do things as opposed to computing new values.

I’d agree with this being nicer to write because you could have pipelines of actions, but uh. Statements in code already accomplish that, and there isn’t anything piped anyway… So I’d argue it’s misleading and not needed.

# misleading:
a = []
b = a.append(3).append(8)
# this suggests a and b are separate, but they refer to the same list
1 Like