Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 got SyntaxError while building simple function
Hi guys,

How can i set up simple function to unpack a list?

i was trying to:
def list_to_string(x):
    x = *x
    return x
but then i can see
SyntaxError: can't use starred expression here
What should this * expression do?

What do you want to achieve?
If you want to concatenate the items in the list to a str, you could use the join method on str.

your_list = ['Hello', 'World']
the_seperator = " "
my_new_str = the_seperator.join(your_list)
Usually it's written more dense:
my_new_str = " ".join(your_list)
If the list does not only contain str objects, you need to convert them.

your_list = ['Hello', 'World', None, True, False, ...]
the_seperator = " "
to_str = map(str, your_list)
# lazy evaluation
# apply str(item) for each item in your_list
my_new_str = the_seperator.join(to_str)

Unpacking arguments in function signature:
def greet(greeting, times, *names):
    for name in names:
        for _ in range(times):

greet("Hello {}.", 2, "zarize", "DeaD_EyE")
#                     ^^^^^^^^^^^^^^^^^^^^^ <- *names

Unpacking keyword arguments in function signature:
def cook(**indigrents):
    for indigrent, how_often in indigrents.items():
        print(indigrent.capitalize(), 'x', how_often)

cook(beer=3, bannana=1)
#     ^^^^^^^^^^^^^^^^ <- **indigrents
zarize likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thank you for your input :)

My goal was to create simple function which would transfer list into a string ideally (just by passing an argument to function and it would return whole list as string)

Lets say i have
mylist = [ABC, 123, 456] 
Ideally i would like to get '123456'

I know that i can do it on many ways such as:
mylist = mylist.split()[1:3]
mylist = ''.join(mylist)
mylist = mylist[1] + mylist[2]

Usually it's written more dense:
Python Code: (Double-click to select all)
my_new_str = " ".join(your_list)

Thank you.. i am dumb :P

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function: SyntaxError: invalid syntax vejin 2 130 Feb-02-2020, 09:25 PM
Last Post: ThiefOfTime
  Simple statistics with range function Pythonlearner2019 2 170 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Cannot get simple i/o to function. jerryi 10 1,590 Jul-27-2019, 06:22 PM
Last Post: jerryi
  Need help with a simple function WorldPark 4 431 Apr-26-2019, 12:28 PM
Last Post: perfringo
  Why this simple function doesnt work? blackknite 8 636 Jan-05-2019, 12:32 PM
Last Post: buran
  def function SyntaxError: invalid syntax Said 1 2,706 Jul-10-2018, 10:16 AM
Last Post: Said
  Simple Function Problem, please help ShadowWarrior17 16 2,144 Jan-03-2018, 09:29 PM
Last Post: ShadowWarrior17
  How to do multithrading of a simple function? ratanbhushan 1 987 Nov-17-2017, 02:32 PM
Last Post: heiner55
  Simple Function Call PappaBear 2 1,269 Apr-04-2017, 11:12 PM
Last Post: PappaBear

Forum Jump:

Users browsing this thread: 1 Guest(s)