- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 – Print the count of either peaks or valleys from a list
When it is required to print the count of either peaks or valleys from a list, a simple iteration and a specific condition is placed.
Below is a demonstration of the same −
my_list = [11,12, 24, 12, 36, 17, 28, 63] print("The list is :") print(my_list) my_result = 0 for index in range(1, len(my_list) - 1): if my_list[index + 1] > my_list[index] < my_list[index - 1] or my_list[index + 1] < my_list[index] > my_list[index - 1]: my_result += 1 print("The result is :") print(my_result)
The list is : [11, 12, 24, 12, 36, 17, 28, 63] The result is : 4
A list is defined and is displayed on the console.
An integer variable is initialized to 0.
The list is iterated over, and the consecutive indices are checked to see if they are less than or greater than each other.
If so, the integer is incremented by 1.
This is displayed as output on the console.
- Python program to print duplicates from a list of integers?
- Program to find indices or local peaks in Python
- Python Program – Create dictionary from the list
- Python Program to print unique values from a list
- Python Program to Print Nth Node from the last of a Linked List
- Python program – All occurrences of Substring from the list of strings
- Python – Average digits count in a List
- Python – Sort a List by Factor count
- Python – Create dictionary from the list
- C# program to print duplicates from a list of integers
- Java program to print duplicates from a list of integers
- Python program to print all sublists of a list.
- Python Program to print element with maximum vowels from a List
- Python – Count frequency of sublist in given list
- Python – All occurrences of Substring from the list of strings