Python Forum
Function parameter not writing to variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function parameter not writing to variable
#1
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!
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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.
Karp likes this post
Reply
#4
(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
Reply
#5
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.
Karp likes this post
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 305 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 668 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 602 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,335 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 594 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,099 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Create a function for writing to SQL data to csv mg24 4 1,179 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing into 2 text files from the same function paul18fr 4 1,690 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Retrieve variable from function labgoggles 2 1,056 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,906 Feb-09-2022, 10:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020