I have written two types of solution.
The duplicate function uses a local variable (func_loc_cheak)(in function scope) and return the content of this local list.
The duplicate_glob function uses a global variable (cheak_global) and it doesn't return anything because you can use this global variable without variable assignment.
I have added several print to see the behaviors and differences between the functions.
Code:
cheak_global = []
def duplicate_glob(any_list):
for things in any_list:
if things not in cheak_global:
cheak_global.append(things)
def duplicate(any_list):
func_loc_cheak = []
for things in any_list:
if things not in func_loc_cheak:
func_loc_cheak.append(things)
return func_loc_cheak
if __name__ == "__main__":
# Test lists.
my_list = [1, 2, 3, 4, 3, 5]
my_other_list = [8, 9, 0]
result = duplicate(my_list)
print("After first function call: {}".format(result))
result = duplicate(my_other_list)
print("After second function call: {}".format(result))
print("Content of 'cheak_global' before function call: {}".format(cheak_global))
duplicate_glob(my_list)
print("Content of 'cheak_global' after first function call: {}".format(cheak_global))
duplicate_glob(my_other_list)
print("Content of 'cheak_global' after second function call: {}".format(cheak_global))
Output:
>>> python3 test.py
After first function call: [1, 2, 3, 4, 5]
After second function call: [8, 9, 0]
Content of 'cheak_global' before function call: []
Content of 'cheak_global' after first function call: [1, 2, 3, 4, 5]
Content of 'cheak_global' after second function call: [1, 2, 3, 4, 5, 8, 9, 0]