I'm attempting to come up with a function that will use a for loop to replicate the .join function. For example, L = [this, is, a, list], function(L, "/"), it would return a string "this/is/a/list". I'm stuck and can't seem to figure out how to add the elements from the list and then turn it into a string. Any advice would be appreciated.
1 reply to this topic
#1
Posted 31 October 2011 - 09:21 PM
|
|
|
#2
Posted 01 November 2011 - 05:14 AM
What part are you having difficulty with? I'll provide you with a possible answer, but I hope that you with to learn.
L = [3, 'little', 'pigs', 'murdered', 19, 'orphans']
def using_for(iterable, seperator=' '):
t = ''
for i in iterable[:-1]:
t += '%s%s' % (i, seperator)
return t + iterable[-1].__str__()
print using_for(L, '/')
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









