Let's suppose that we have a list like input_list = [1, 2, 3, 4, 5, 6] and we want to retrieve from it 3 variables, a=1, b=[2, 3, 4, 5] and c=6. A primitive solution to that problem could be:
a, b, c = input_list[0], input_list[1:len(input_list)-1], input_list[-1] assert(a == 1) assert(b == [2, 3, 4, 5]) assert(c == 6)
Fortunately, as a lot of things in Python, we can write this snippet in a more concise way by using unpacking:
a_unpacked, *b_unpacked, c_unpacked = input_list assert(a_unpacked == 1) assert(b_unpacked == [2, 3, 4, 5]) assert(c_unpacked == 6)
Similarly, you can retrieve the first or the last elements of your list that way:
a_unpacked_last, b_unpacked_last, *c_unpacked_last = input_list assert(a_unpacked_last == 1) assert(b_unpacked_last == 2) assert(c_unpacked_last == [3, 4, 5, 6])