Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

python - Getting the next element of the list with for loop

I've list and I want to get the other element when I found the value I'm looking for. For example:

list = [1,2,3,4,5,6]
for element in list:
    if element > 3:
        variable = element
        break
print(element)

4

So I want to get the 5. Is there a function to do that ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
result = [a for a in list if a > 3]

This returns an array of all elements you're looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...