samedi 15 novembre 2014

How do you iterate over a list and produce the desired result as a tuple


Vote count:

0




I have a question, I been trying to get this output. And I also asked a friend. But we can't get the desired output. His results is closer then mine. Anyway, my problem is that, I do not know how to iterate across the whole tuple as expected. I yet add 'List is'. My primary concern is that, am stuck at 'r,' and 'r-' and 'None'. Sorry, if my grammer is out of the window, coz it's kinda in the early morning.


Supposed output as below:



to_string Test
List is: r, i, n, g, i, n, g
List is: r-i-n-g-i-n-g
List is:


My input:



def to_string(my_list,sep=','):

result = ''
index = length(my_list)

for char in range(index):

result += (str((my_list[char]) + sep))

return result

my_list = ['r', 'i', 'n', 'g', 'i', 'n', 'g']
print(to_string(my_list, ''))


My friend's input:



#2
def to_string(my_list,sep=','):
print("List is :",end="")

index=len(my_list)
for num in range(index):
if num<index-1:
print((str(my_list[num]))+sep,end="")
elif num==index-1:
print(str(my_list[num]),end="")

print("")


asked 40 secs ago







How do you iterate over a list and produce the desired result as a tuple

Aucun commentaire:

Enregistrer un commentaire