Reputation: 101
In a function, I need to return a tuple consisting the first 3 and last 3 elements of the argument. I have tried the min and max but i need to get (10,20,30,70,80,90)
so for example:
if the function is called with the tuple (0,10,20,30,40,50,60,70,80,90) as argument, the function supposed to return (10,20,30,70,80,90). Can someone please explain to me or give me a hint on what should I do?
this is my current code:
def first3_last3(t):
return min(t), max(t)
t = (10,20,30,40,50,60,70,80,90)
print(first3_last3(t))
Upvotes: 2
Views: 505
Reputation: 16908
You can also use the splat operator *
to merge the sliced tuples:
def first3_last3(t):
return (*t[:3], *t[-3:])
t = (10,20,30,40,50,60,70,80,90)
print(first3_last3(t))
Output:
>> (10, 20, 30, 70, 80, 90)
Upvotes: 4
Reputation: 3828
Sorting (if it's unsorted) and using slicing gives you the output you are looking for.
def first3_last3(t):
t = sorted(t)
return tuple(t[:3] + t[-3:])
t = (10,20,30,40,50,60,70,80,90)
print(first3_last3(t))
returns
(10, 20, 30, 70, 80, 90)
Upvotes: 3