Function parameter not writing to variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Function parameter not writing to variable (/thread-40495.html) |
Function parameter not writing to variable - Karp - Aug-06-2023 Hi, I just started learning python and have run into a problem with writing a function recombining a list of separated characters into a string. When running the function nothing gets added to the variable that is put in as the combined parameter (output_string), resulting in nothing getting printed in print(output_string). Why no work? input_list = ["h", "e", "l", "l", "o"] output_string = "" def list_comb(separated, combined): for letter in separated: combined = combined + letter list_comb(input_list, output_string) print(output_string)Thanks! RE: Function parameter not writing to variable - menator01 - Aug-06-2023 Your not returning or printing anything from the function call input_list = ["h", "e", "l", "l", "o"] def list_comb(mylist): print(f'Print in the function -> {"".join(mylist)}') return ''.join(mylist) print(f'Returned from function call -> {list_comb(input_list)}') RE: Function parameter not writing to variable - snippsat - Aug-06-2023 To fix you original code. def list_comb(separated, combined): for letter in separated: combined += letter return combined input_list = ["h", "e", "l", "l", "o"] output_string = "" result = list_comb(input_list, output_string) print(result) As posted over this is what "".join(lst) do,as training it's a ok task.
RE: Function parameter not writing to variable - Karp - Aug-07-2023 (Aug-06-2023, 07:54 PM)menator01 Wrote: Your not returning or printing anything from the function call Thanks! Makes sense Out of curiosity though, the bellow function works even though there is not a print or return (to me it feels like it is the same as the previous example where it changes external data outside of the function when running it. I assume they are different, but feel like they are doing the same thing (only difference being one changing a string and one changing a list)). What is the difference that makes one work and not the other? input_string = "hello" output_list = [] def separate_char(combined, separated): for symbol in combined: separated.append(symbol) separate_char(input_string, output_list) print(output_list) Thanks again! Hope the question makes sense
RE: Function parameter not writing to variable - deanhystad - Aug-07-2023 Lists are mutable (they can be modified) and Python passes arguments by object. When you pass a mutable object to a function, the function can modify the mutable object. Since the changes are to the object, those changes are seen outside the function. In your example, you passed a mutable object, "output_list" to a function. The function modified "output_list", adding strings to the list. Now anyone looking at the list will see the modified contents. Strings are not mutable. You cannot change a string. When you add two strings it creates a new string. In your first example, "combined = combined + letter" does not modify the string referenced by "combined", it creates a brand new string and assigns "combined" to reference the new string. Since the original blank string passed to the function cannot be changed, and output_string has not been reassigned to reference a different object, output_string still reference the original, blank string. RE: Function parameter not writing to variable - Karp - Aug-07-2023 (Aug-07-2023, 01:03 PM)deanhystad Wrote: Lists are mutable (they can be modified) and Python passes arguments by object. When you pass a mutable object to a function, the function can modify the mutable object. Since the changes are to the object, those changes are seen outside the function. In your example, you passed a mutable object, "output_list" to a function. The function modified "output_list", adding strings to the list. Now anyone looking at the list will see the modified contents. Thank you so much! Great explanation |