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
CodePudding user response:
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'
CodePudding user response:
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
CodePudding user response:
Try -
mark_list1 = ["May", 56, 65, 60, 66, 62]
mark_list2 = ["Jess", 81, 86, 85]
def get_marks(lst):
a = '-'.join([str(i) for i in lst[1:]])
result = lst[0] ':' a
return result
print(get_marks(mark_list1)) # May:56-65-60-66-62
print(get_marks(mark_list2)) # Jess:81-86-85