
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Equidistant consecutive characters Strings
When it is required to find equidistant consecutive character strings, a list comprehension, the ‘all’ operator and the ‘ord’ method are used.
Example
Below is a demonstration of the same
my_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms'] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(ord(sub[index + 1]) - ord(sub[index]) == ord(sub[1]) - ord(sub[0]) for index in range(0, len(sub) - 1))] print("The resultant list is :") print(my_result)
Output
The list is : ['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms'] The resultant list is : ['abc', 'mpsv', 'agms']
Explanation
A list of string values is defined and is displayed on the console.
A list comprehension is used to iterate through the list.
The ‘all’ operator along with ‘ord’ method is used to check if the difference between next index and current index is equivalent to the first index and zeroth index.
This is assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Python Program to Split joined consecutive similar characters
- Python – Strings with all given List characters
- Python – Filter Tuples with Strings of specific characters
- Python Program – Strings with all given List characters
- Convert list of strings and characters to list of characters in Python
- Program to find string after removing consecutive duplicate characters in Python
- Python Program to Count number of binary strings without consecutive 1’
- Program to find length of substring with consecutive common characters in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to replace all question symbols to avoid consecutive repeating characters in Python
- Python code to print common characters of two Strings in alphabetical order
- Matching strings for similar characters - JavaScript
- Program to equal two strings of same length by swapping characters in Python
- Print consecutive characters together in a line in C++
- Longest string consisting of n consecutive strings in JavaScript

Advertisements