[Tutor] Convert a list to a group of separated strings

Peter Otten __peter__ at web.de
Thu Sep 11 19:09:19 CEST 2014


Juan Christian wrote:

> Let's say I have the following list: my_list = ['76561198048214059',
> '76561198065852182', '76561198067017670', '76561198077080978',
> '76561198077257977', '7656119807971
> 7745', '76561198088368223', '76561198144945778']
> 
> and I have a function with the following signature: def
> fetch_users(*steamids)
> 
> Usage: fetch_users("76561198048214059", "76561198065852182",
> "76561198067017670", [...])
> 
> 
> Inside the 'fetch_users' function I have a call to the API using this
> approach: req = urllib.request.urlopen('API_URL_HERE&steamids=' +
> ','.join(steamids))
> 
> This way I call the API with the following URL (example):
> 
> API_URL_HERE&steamids=76561198048214059,76561198065852182
> 
> And everything works.
> 
> The problem is that when I give the 'my_list' directly to 'fetch_users'
> function it gives me "TypeError: sequence item 0: expected str instance,
> list found".
> 
> How can I convert it to something like "fetch_users("76561198048214059",
> "76561198065852182", "76561198067017670", [...])"
> 
> I tried with " ''.join(my_list) " but didn't work. I think it's a very
> simple question but I'm struggling to find the answer.

You can explode the list by invoking the function with

fetch_users(*my_list) # prepend the list with a star

but I recommend that you change the function's signature to

def fetch_users(steamids):
    ...

and invoke it in the common way with

fetch_users(my_list)

That of course means that even to invoke the function with a single user you 
have to wrap the user's id in a list:

fetch_users(["76561198048214059"])



More information about the Tutor mailing list