- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Python program to remove each y occurrence before x in List
- Python – Test if list is Palindrome
- Program to count number of flipping required to make all x before y in Python
- Why did changing list ‘y’ also change list ‘x’ in Python?
- Python program to multiply all numbers in the list?
- Python – Test if tuple list has a single element
- Program to check whether given list of blocks are symmetric over x = y line or not in python
- Python – Test if elements of list are in Min/Max range from other list
- Python – Test for all Even elements in the List for the given Range
- Python program to print all sublists of a list.
- How to test if a List is an Unmodifable List in Java?
- Python – Test if all elements are unique in columns of a Matrix
- Check if list contains all unique elements in Python
- Python program to get all pairwise combinations from a list
- Python program to check if all the values in a list that are greater than a given value
