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
Download my project scripts


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
  function as parameter Azdaghost 2 745 Apr-01-2025, 07:10 PM
Last Post: DeaD_EyE
  not able to call the variable inside the if/elif function mareeswaran 3 562 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,426 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 2,935 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,604 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 7,405 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 1,242 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 17,587 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Create a function for writing to SQL data to csv mg24 4 2,526 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing into 2 text files from the same function paul18fr 4 2,668 Jul-28-2022, 04:34 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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