Reputation:
How can I return a string for the following list:
mark_list1 = ["May", 56, 65, 60, 66, 62]
mark_list2 = ["Jess", 81, 86, 85]
Expected output:
May:56-65-60-66-62
Jess:81-86-85
I have tried doing the following but it I'm stuck since it won't work for all cases:
def get_marks(mark_list):
for x in mark_list:
string = mark_list[0] + ":" + str(mark_list[1])
return string
Upvotes: 0
Views: 180
Reputation: 81
I have a better idea,I've tried it and I can get what you want
mark_list1 = ["May", 56, 65, 60, 66, 62]
result = mark_list1[0]+":"+'-'.join([str(_) for _ in mark_list1[1:]])
print(result)
>>> 'May:56-65-60-66-62'
This is the picture of my demo
Upvotes: 0
Reputation: 59390
You can use a generator expression or map
with join
:
def get_marks(mark_list):
name, *nums = mark_list
return f"{name}:{'-'.join(map(str, nums))}"
then
>>> get_marks(["May", 56, 65, 60, 66, 62])
'May:56-65-60-66-62'
Upvotes: 3