Del inside a Function

Hi. Is the del function unusable inside a function? As the below code produces an error:

my_list = ['Mary','had', 'a', 'little', 'lamb']

def my_list (my_list):
    del my_list [3]
    my_list[3] = 'ram'
        
print(my_list(my_list))

If you write def my_list (my_list):, what do you think this should mean? When you write print(my_list(my_list)), what do you think this should mean? Specifically: what should my_list mean each time it appears in this line of the code? Should it be different each time? Why and how do you expect that to work?

Any form of element changes, such as del or value changes, are not allowed inside a function? Is that right?

No, that is not right at all. You can do the same things inside a function as outside.

The problem is that you are trying to use the same name for the function as for the list. Things can have many names, but a name can only have one thing that it’s naming at a time. if my_list means the function, it cannot also mean the list.

I see… Now i tried to modify the function name to be different from the list… Why is the result None? Confused…

my_list = ['Mary','had', 'a', 'little', 'lamb']

def func_my_list(my_list):
    del my_list[3]
    my_list[3] = 'ram'
        
print(func_my_list(my_list))

Your function changes the list, but does not return it. print(func_my_list(my_list)) means to print whatever is returned from func_my_list when you call it with my_list. It does not mean to call func_my_list with my_list and then print my_list.

OOHHH… I get it now

my_list = ['Mary','had', 'a', 'little', 'lamb']

def func_my_list(my_list):
    del my_list[3]
    my_list[3] = 'ram'
        
print(func_my_list(my_list))
print(my_list)

Thank you!