

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to test if all y occur after x in List
When it is required to check if all ‘y’ occurs after ‘x’ in a list, the enumerate attribute along with a specific condition is used.
Example
Below is a demonstration of the same
my_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False break if(my_result == True): print("All y elements occcur after x elements") else: print("All y elements don't occcur after x elements")
Output
The list is : [11, 25, 13, 11, 64, 25, 8, 9] All y elements occcur after x elements
Explanation
A list is defined and is displayed on the console.
Two integer variables are initialized.
The index values of the elements of the list are stored in a variable.
A variable is set to Boolean ‘True’.
The elements and the indices of the list are iterated using enumerate.
Inside this, if the element being iterated and the second integer are equivalent and the index being iterated is less than index of the second integer, then the temporary variable is set to Boolean ‘False’.
The control breaks out of the loop.
In the end, based on the value of the temporary variable, the relevant message is displayed on the console.
- Related Questions & Answers
- Python program to remove each y occurrence before x in List
- Program to count number of flipping required to make all x before y in Python
- Python – Test if list is Palindrome
- Check if a number can be expressed as x^y (x raised to power y) in C++
- Find larger of x^y and y^x in C++
- Program to check whether given list of blocks are symmetric over x = y line or not in python
- C Program to check if the points are parallel to X axis or Y axis
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Python – Test if tuple list has a single element
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- How to test if a List is an Unmodifable List in Java?
- Program to find value of find(x, y) is even or odd in Python
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Check if list contains all unique elements in Python
- Python – Test if elements of list are in Min/Max range from other list