0

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
Selcuk
  • 57,004
  • 12
  • 102
  • 110

2 Answers2

3

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'
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Just tested it using `timeit.timeit('".".join(map(str, range(1000)))')` vs `timeit.timeit('".".join([str(i) for i in range(1000)])')` and list comprehension is slightly slower (about 14% on my system). Also uses more space as it needs to construct a list first. – Selcuk Sep 30 '21 at 02:46
  • https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function I've got `".".join(list(map(str, range(1000)))` about 15% faster than one without `list` – Chris Sep 30 '21 at 03:38
  • 1
    Weird, I still get slightly lower performance (this time the difference is less than 1% but still slower). That question is about Python 2.7, maybe they optimised `.join()` in Python 3.8 (which I'm currently using to test this). – Selcuk Sep 30 '21 at 03:52
  • 1
    Hm.. weird. I've tested on 3.5 and 3.8 and listifying seems faster. I'm confused :P I'll dig into it. But anyway, great answer :) – Chris Sep 30 '21 at 05:20
0

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

enter image description here

Jary
  • 81
  • 4
  • 1
    This would work, but note that `_` is typically used for variables that are discarded (i.e. not used). If you are going to use it (as you did here) you should use a proper variable name. – Selcuk Sep 30 '21 at 02:09
  • I don't think `_` should only be used to define unused variables. If there is only one variable in a list derivation, I prefer to name it `_` – Jary Sep 30 '21 at 02:15
  • 2
    You are free to use any naming convention you like, but the general consensus is that it's a [throwaway variable](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python). Other Python developers will be surprised when reading your code. – Selcuk Sep 30 '21 at 02:54
  • Yes, I know it's a throwaway variable that I won't use anywhere else, so I call it `_` and I don't have to worry about naming it. I only use it once and then throw it away – Jary Sep 30 '21 at 03:00
  • 1
    `Yes, I know it's a throwaway variable`: But you are not throwing it away. `I won't use anywhere else`: You can't use a list comprehension variable anywhere else anyway (at least not after Python 3). – Selcuk Sep 30 '21 at 03:05
  • After this operation.I have thrown it, and I will never use it again – Jary Sep 30 '21 at 03:10
  • Using _ is confusing because it is in fact being used to generate something inside the comprehension. – Tarik Sep 30 '21 at 06:54