python - Take a segment from a list -


In addition to using a loop, there is a way to take a block from a list

for example For, if I had the list:

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

And I wanted to take a break from the third item to the 5th, ending with the i ...

  Segment = [4,5,6]]   

I know that it can be done with a loop, but I was wondering if there was a more "python" arousal?

l [3: 6] is what you are looking for.

This list uses skiing 3 The start index is 6 the end index is we end index as 6 (Even if index 6 is 6) because the piece signaling requires the index of the element after the last element, so that we should be part of the segment.

Demo:

  & gt; & Gt; & Gt; L = [1,2,3,4,5,6,7,8,9,0] & gt; & Gt; & Gt; L [3: 6] [4, 5, 6]   

This answer explains how python functions to work in the list - < / Html>

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -