Python program to omit K length Rows

PythonServer Side ProgrammingProgramming

When it is required to omit K length rows, a simple iteration and the ‘len’ method along with ‘append’ method are used.

Example

Below is a demonstration of the same

my_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]]

print("The list is :")
print(my_list)

my_k = 2
print("The value of K is ")
print(my_k)

my_result = []

for row in my_list:
   if len(row) != my_k :
      my_result.append(row)

print("The resultant list is :")
print(my_result)

Output

The list is :
[[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]]
The value of K is
2
The resultant list is :
[[8, 10, 12, 8], [6, 82, 10]]

Explanation

  • A list of list is defined and is displayed on the console.

  • A key values defined and is displayed on the console.

  • An empty dictionary is created.

  • The list is iterated over.

  • If the length of the specific list is not equal to key value, it is appended to the empty list.

  • This is the output that is displayed on the console.

raja
Published on 14-Sep-2021 11:21:15
Advertisements