Posts: 3
Threads: 1
Joined: Aug 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!
Posts: 1,145
Threads: 114
Joined: Sep 2019
Aug-06-2023, 07:54 PM
(This post was last modified: Aug-06-2023, 08:00 PM by menator01.)
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)}')
Karp and Gribouillis like this post
Posts: 7,319
Threads: 123
Joined: Sep 2016
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) Output: hello
As posted over this is what "".join(lst) do,as training it's a ok task.
Posts: 3
Threads: 1
Joined: Aug 2023
(Aug-06-2023, 07:54 PM)menator01 Wrote: 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)}')
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) Output: ['h', 'e', 'l', 'l', 'o']
Thanks again! Hope the question makes sense
Posts: 6,797
Threads: 20
Joined: Feb 2020
Aug-07-2023, 01:03 PM
(This post was last modified: Aug-07-2023, 01:03 PM by deanhystad.)
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.
Posts: 3
Threads: 1
Joined: Aug 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.
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.
Thank you so much! Great explanation
|