Python Forum
got SyntaxError while building simple function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
got SyntaxError while building simple function
#1
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
Reply
#2
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)
print(my_new_str)
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)
print(my_new_str)

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


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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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)
or:
mylist = mylist[1] + mylist[2]
SOLVED:

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



Thank you.. i am dumb :P
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,751 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Building a method name in a function ffgth 9 3,132 Oct-19-2020, 01:21 PM
Last Post: buran
  Learning python SyntaxError: 'return' outside function Grale1953 3 2,467 Aug-03-2020, 06:55 AM
Last Post: buran
  Function: SyntaxError: invalid syntax vejin 2 2,303 Feb-02-2020, 09:25 PM
Last Post: ThiefOfTime
  Simple statistics with range function Pythonlearner2019 2 2,082 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Cannot get simple i/o to function. jerryi 10 6,614 Jul-27-2019, 06:22 PM
Last Post: jerryi
  Need help with a simple function WorldPark 4 2,618 Apr-26-2019, 12:28 PM
Last Post: perfringo
  Why this simple function doesnt work? blackknite 8 3,904 Jan-05-2019, 12:32 PM
Last Post: buran
  def function SyntaxError: invalid syntax Said 1 5,878 Jul-10-2018, 10:16 AM
Last Post: Said
  Simple Function Problem, please help ShadowWarrior17 16 6,926 Jan-03-2018, 09:29 PM
Last Post: ShadowWarrior17

Forum Jump:

User Panel Messages

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