Join all except last x in list

2020-08-13 04:31发布

问题:

I have a list and want to join all except the last 2 entries i.e.

x = ["1", "2", "3", "4"]
print ', '.join(x[from 0 until -3])

Output would then be 1, 2. How could I achieve this?

回答1:

Use slicing

>>> x = ["1", "2", "3", "4"]
>>> print ', '.join(x[:-2])
1, 2