0

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 ?

8
  • Not sure what you mean by "the other element" you mean the next element after the one you found? Commented Jan 12, 2021 at 20:23
  • you use break statement when your condition is complete and you want to exit the loop, in your code as you are looking other elements also for the condition to satisfy, don't use a loop, use print to see which element is greater than 3 or append all element >3 in a list and then prin the list out later. Commented Jan 12, 2021 at 20:27
  • @AndrewAllaire yes Commented Jan 12, 2021 at 20:28
  • @sahasrara62 Okay, i've got that. Thanks. Commented Jan 12, 2021 at 20:29
  • @Skyaigrette can you provide the final output which you want Commented Jan 12, 2021 at 20:56

4 Answers 4

1
result = [a for a in list if a > 3]

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

Sign up to request clarification or add additional context in comments.

Comments

0

I am not entirely sure what you mean. Do you mean that you want to skip one element and return the second element that is found? If that is the case, your code should be

list = [1,2,3,4,5,6]
found1 = false
for element in list:
    if element > 3:
        if(!found1):
           found1 = true;
        else:
           variable = element
           break

print(variable)

2 Comments

@sahasrara62 Actually I think he wants the next element after the first one that is greater than 3. Otherwise in his example he would say he wanted [4, 5, 6] and not say he wanted 5. Also I asked him to clarify in a comment and he seemed to verify my understanding. Of course there might be a communication problem still, the question is oddly worded I think.
@AndrewAllaire yeah, it's will be more clear if OP clarify the final output he wants, till then it's wrong to pre-assume anything
0

One way you could do this is to use an index, i. By using enumerate I get back both the element and its index in the list. I call the list my_list rather than list since list is a reserved word already. Also using [:-1] to go one short of end of list, since if the last element matches there is no next element.

Other than that I tried to keep everything the same as you did it.

variable = None

my_list = [1,2,3,4,5,6]
for i, element in enumerate(my_list[:-1]):
    if element > 3:
        variable = my_list[i + 1]
        break
print(variable)

Comments

0

The simplest solution I can think of:

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

This always shows the element that comes after the element that first satisfies your condition.

If you happen to know the exact value you're looking for, then simply use it as a parameter of index(), like this, to show the element coming after that:

list[list.index(4)+1]

But you can't use an if condition this way, you can only look up a specific value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.