Related Articles
Python program to get the next unrevealed element for the given element
• Last Updated : 11 Jun, 2021

Given a list of numbers and a particular element from the list, the task is to write a Python program to get the next unrevealed element to the first occurrence of a given element i.e element that does not occur till the given element.

Examples:

Input : test_list = [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6], nex_to_ele = 6

Output : 2

Explanation : In the above list there are 2, 3, 4, 5, and 6 are elements, as nex_to_ele is 6, whose first occurance is at index 2, before that only 3 and 4 occurs, so the unrevealed elements are 2 and 5, out of which unrevealed element 2 is next after the first occurance of 6. Hence 2 is the next unrevealed element of 6 in the list.

Input : test_list = [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6], nex_to_ele = 3

Output : 4

Explanation : Next different unrevealed element to 3 is 4 as 3 occurs at index 0. So, none of the elements in the list have occured before 3 and 4 is the next element after 3.

Method #1 : Using set() + sorted() + index()

In this, the list is converted to a unique element list preserving order using set() and sorted(). Then index() is used to get the index of number and the next element to it in the result list is the required element.

## Python3

 `# Python3 code to demonstrate working of``# Next different element in list``# Using set() + sorted() + index()` `# initializing list``test_list ``=` `[``3``, ``4``, ``6``, ``6``, ``3``, ``2``, ``3``, ``5``, ``6``, ``3``, ``4``, ``5``, ``5``, ``3``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing element``nex_to_ele ``=` `6` `# sorting removing duplicated keeping order``temp ``=` `sorted``(``set``(test_list), key``=``lambda` `sub: test_list.index(sub))` `# getting next index``num_idx ``=` `temp.index(nex_to_ele)` `# checking for last index for overflow``if` `num_idx ``=``=` `len``(temp) ``-` `1``:``    ``res ``=` `None` `# getting next index as result``else``:``    ``res ``=` `temp[num_idx ``+` `1``]` `# printing result``print``(``"Next different element : "` `+` `str``(res))`

Output:

The original list is : [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6]

Next different element : 2

Method #2 : Using next() + iter() + sorted() + set()

In this, we don’t use index() to get element index, rather user iterator approach to increment to next the iterator till the element is found. The next element to it is result.

## Python3

 `# Python3 code to demonstrate working of``# Next different element in list``# Using next() + iter() + sorted() + set()` `# initializing list``test_list ``=` `[``3``, ``4``, ``6``, ``6``, ``3``, ``2``, ``3``, ``5``, ``6``, ``3``, ``4``, ``5``, ``5``, ``3``, ``6``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing element``nex_to_ele ``=` `6` `# sorting removing duplicated keeping order``temp ``=` `sorted``(``set``(test_list), key``=``lambda` `sub: test_list.index(sub))` `temp_itr ``=` `iter``(temp)``flag ``=` `0``for` `idx ``in` `range``(``0``, ``len``(temp)):` `    ``# if last element is element to find``    ``if` `idx ``=``=` `len``(temp) ``-` `1``:``        ``flag ``=` `1` `    ``# breaking when element is found``    ``if` `next``(temp_itr) ``=``=` `nex_to_ele:``        ``break` `if` `flag:``    ``res ``=` `None``else``:` `    ``# next element is answer``    ``res ``=` `next``(temp_itr)` `# printing result``print``(``"Next different element : "` `+` `str``(res))`

Output:

The original list is : [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6]

Next different element : 2

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up