Program to print the repeated elements in the list using Python

Introduction

In this section of python programming, we will understand the code to print repeated elements in the list.

Program to print the repeated elements in the list

Program

def rep(input_list):
    length = len(input_list)
    rep_input = []
    for i in range(length):
        j = i + 1
        for k in range(j, length):
            if input_list[i] == input_list[k] and input_list[i] not in rep_input:
                rep_input.append(input_list[i])
    return rep_input
   
input_list = [2, 2, 3, 5,4,3,2,10,6,10,8,2,3,10,8]
print("The repeated elements are:", rep(input_list))

 

Output

Program to print the repeated elements in the list Output

Explanation

In the above python program, we have used two nested for loops. The two adjacent elements are checked if they are equal and also whether the element already exists in repeated element list ( which we have created to store the repeated elements). If the element is found repeated, the value is appended to rep_input list. After all the elements are traversed, the list is printed on the screen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.