0

This works in the context of the lesson [Codecademy].

 n = ["Michael", "Lieberman"]
    # Add your function here
def join_strings(lst):
    concat = ""
    for string in lst:
        concat += string
    return concat

However, when I do this in regular Python, outside of the Codecademy environment, and I pass multiple arguments to the function, it returns an error. I was wondering how to make sure that the parameter of the function is a list. This way, I can pass however many arguments I want and the function will simply understand that this is a list.

Note, I do not want to do this with *args or something similar. I was simply wondering if there's a way to pass an actual list into a function.

Would you have to do something like a dictionary?

user3072758
  • 177
  • 1
  • 1
  • 9
  • 1
    But that is what `*args` is for. Otherwise you pass a single list, like you have done. – Daniel Roseman Dec 25 '13 at 14:19
  • What's wrong with `join_strings(n)`? – Maciej Gol Dec 25 '13 at 14:19
  • This is the only way to do it then? Or actually passing a list in the function itself as @rednaw did below? Or...defining your lists as variables and then passing the variables to the function? I guess then in the future I might have to update this to take the arbitrary argument instead. But thanks everyone for the comments. Beginning programmer here. – user3072758 Dec 25 '13 at 14:21
  • You are not showing us how you are calling the function or what the error is. – dansalmo Dec 25 '13 at 16:59

4 Answers4

0

To pass a list to a function:

join_strings(["Michael", "Lieberman"])
gitaarik
  • 42,736
  • 12
  • 98
  • 105
0

Use asteric operator -

def join_strings(*lst):

Your function will interpret the positional parameters as list

volcano
  • 3,578
  • 21
  • 28
0

You can pass the list as argument to the function for example in your program you can pass the list like this

n = ["Michael", "Lieberman"]
join_strings(n)

The above code passes list n to join_strings function

To check whether the passed argument is list or not you can have a checking condition like this

def join_strings(lst):
    if isinstance(lst, list):
       #do something
    else:
       # do something else

in the above code isinstance() method is used to check whether the passed argument is list or not

Vineeth Guna
  • 388
  • 4
  • 10
  • This isn't a bad answer in itself, but the asker wanted to know if the function could be written so that multiple arguments are given, and then interpreted as a list. – SimonT Dec 25 '13 at 16:51
0

I’m sorry guys, apparently I did something wrong when I was typing it into the Terminal or something. Or possibly the way that Sublime (used to) interact with the pre-installed Python stuff on Mavericks. It works fine now, from Sublime and from the Terminal. I’m really not sure what the original problem was or where it was coming from.

— Typing the code in as I had it the first time gave me exactly what I wanted, and then I tweaked it to add a space between the concat strings.

def join_strings(lst):
    concat = ""
    for string in lst:
        concat += (string + " ")
    return concat

Thanks for all the help, though. Sorry to bother. :)

user3072758
  • 177
  • 1
  • 1
  • 9