Eliminating repeated lines from a file using Python Last Updated : 19 Dec, 2025 Comments Improve Suggest changes Like Article Like Report Given a text file that contains several duplicate lines, the task is to remove all repeated lines and produce an output file containing only unique lines, while keeping their original order.Example: Input file(myfile.txt)This is a sample line.Python is a powerful language.This is a sample line.Output:This is a sample line.Python is a powerful language.Below are several methods to eliminate repeated lines from a file:Using a SetThis method removes duplicate lines by storing only unique lines in a Python set. Python seen = set() with open("myfile.txt", "r") as f_in, open("output.txt", "w") as f_out: for ln in f_in: if ln not in seen: f_out.write(ln) seen.add(ln) Output This is a sample line.Python is a powerful language.Explanation:seen = set(): Stores all unique lines encounteredfor ln in f_in: Reads every line one by oneif ln not in seen: Checks if the line is uniquef_out.write(ln): Writes unique line to output fileseen.add(ln): Marks the line as seen.Using a ListThis method removes repeated lines by checking each line before adding it to a list, ensuring only unique lines are kept. C++ seen = [] with open("myfile.txt", "r") as f_in, open("output.txt", "w") as f_out: for ln in f_in: if ln not in seen: f_out.write(ln) seen.append(ln) OutputThis is a sample line.Python is a powerful language.Explanation:f_out.write(ln): Writes only unique linesseen.append(ln): Saves the line for comparisonUsing PandasThis method removes duplicate lines by loading the file into a Pandas DataFrame and using its built-in drop_duplicates() function. Python import pandas as pd df = pd.read_csv("myfile.txt", header=None) df.drop_duplicates(inplace=True) df.to_csv("output.txt", index=False, header=False) OutputThis is a sample line.Python is a powerful language.Explanation:read_csv(...): Reads text lines into a DataFramedrop_duplicates(): Removes duplicate rowsto_csv(...): Saves cleaned data back to a fileRelated Articles:Python - Remove Duplicates from a ListPython | Remove all Duplicates Words from a given Sentence Create Quiz Comment E equbalzeeshan Follow 0 Improve E equbalzeeshan Follow 0 Improve Article Tags : Python python-file-handling Python file-handling-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like