how to join a certain string values in a list

Using unpacking:

[*my_lst[:2], set(my_lst[2:])]

Output:

['a', 'b', {'c', 'd', 'e', 'f', 'g'}]

You can make the last element as a whole string with str:

[*my_lst[:2], str(set(my_lst[2:]))]

Output:

['a', 'b', "{'f', 'd', 'e', 'c', 'g'}"]

Leave a Comment