0

Say I have a list:

[0,1,2,3,4,5,6,7,8,9]

From this list I want to retrieve the centre elements at positions 4, 5 and 6. So: [4,5,6] is the output I want from this list.

I know how to remove elements, that's easy using this method:

x = [position1:] + [:position2]

But I don't know how to find those items.

Silver
  • 67
  • 9

1 Answers1

1

Use a slice:

l = [0,1,2,3,4,5,6,7,8,9]
print(l[4:7]) # [4, 5, 6]
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • Yep... simple as that. Thankyou, I'll remove because I couldn't find the other answer before, but it's much more extensive and answers this much better. – Silver Oct 04 '17 at 17:09