- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program that print elements common at specified index of list elements
When it is required to print the elements common at a specific index in a list of strings, a ‘min’ method, list comprehension and a Boolean flag value can be used.
Below is a demonstration of the same
my_list = ["week", "seek", "beek", "reek", 'meek', 'peek'] print("The list is :") print(my_list) min_length = min(len(element) for element in my_list) my_result =  for index in range(0, min_length): flag = True for element in my_list: if element[index] != my_list[index]: flag = False break if flag: my_result.append(my_list[index]) print("The result is :") print(my_result)
The list is : ['week', 'seek', 'beek', 'reek', 'meek', 'peek'] The result is : ['e', 'e', 'k']
A list of strings is defined and is displayed on the console.
The list comprehension is used to iterate through elements of the list and get the minimum of lengths of elements.
This is assigned to a variable.
An empty list is defined.
The list is iterated over, and a Boolean value is assigned to 'True'.
The elements of the list are iterated again, and if the element at a specific index is not equal to character at a specific index, the Boolean value is assigned to 'False'.
The control breaks out of the loop.
Depending on this Boolean value, the character is appended to the empty list.
This is displayed as output on the console.
- Related Articles
- Python program to print all the common elements of two lists.
- Python program to print elements which are multiples of elements given in a list
- How to insert the elements of a collection into the List at the specified index in C#?
- Python – Check if elements index are equal for list elements
- Python program to return rows that have element at a specified index
- Equate two list index elements in Python
- Python program to remove elements at Indices in List
- Program to find highest common factor of a list of elements in Python
- Find common elements in list of lists in Python
- Copy OrderedDictionary elements to Array instance at the specified index in C#
- C# program to print all the common elements of two lists
- Python Program to print elements of a tuple
- Java Program to insert all elements of other Collection to specified Index of ArrayList
- Python - Index Directory of Elements
- Python - Index Ranks of Elements