Class 12 Programs
- 1.Read text file line by line with # separator
- 2.Count vowels, consonants, uppercase, lowercase in file
- 3.Remove lines containing character 'a'
- 4.Binary file with name and roll number search
- 5.Binary file update marks by roll number
- 6.Random number generator (Dice Simulator)
- 7.Stack implementation using list
- 8.CSV file with user-id and password
- 9.User Defined Functions to manipulate List Store Indices of Non Zero elements &Double the Odd values
- 10.WORKING WITH BINARY FILE IN PYTHON Create a binary file, Search and display the records from the binary file
- 11.TEXT FILES IN PYTHON Remove duplicate lines from the file & Display unique words present in the file
- 12.TEXT FILES IN PYTHON Copying lines to a new file & Replacing the '-' sign with a blankspace
- 13.TEXT FILES IN PYTHON Count the lines starts with I/T & Display the lines with exactly 6 words
- 14.TEXT FILES IN PYTHON Count the occurrences of word & Count number of vowels and consonants
- 15.User Defined Functions to Manipulate List Find the Longest Word and Shifting the elements to left
- 16.IMPLEMENTATION OF STACK USING LIST IN PYTHON
- 17.ALTER table to add new attributes / modify data type / drop attribute
- 18.UPDATE table to modify data
- 19.ORDER By to display data in ascending / descending order
- 20.DELETE to remove tuple(s)
- 21.GROUP BY and find the min, max, sum, count and average
- 22.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - DELETE RECORDS
- 23.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - UPDATE RECORDS
- 24.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - INSERT RECORDS
Quick Tips
- • Import random module for number generation
- • Use randint(1, 6) for dice simulation
- • Handle user input validation properly
Text File Remove Duplicate
AIM
To write a Python program to remove duplicate lines from a text file.
ALGORITHM
- Start
- Open the input text file in read mode.
- Read all lines from the file and store them in a set to remove duplicates.
- Open a new output text file in write mode.
- Write the unique lines from the set to the output file.
- Close both files.
- Stop
PROGRAM
# Python program to remove duplicate lines from a text file
def remove_duplicates(input_file, output_file):
with open(input_file, 'r') as file:
lines = file.readlines()
unique_lines = set(lines)
with open(output_file, 'w') as file:
file.writelines(unique_lines)
# Example usage
remove_duplicates('input.txt', 'output.txt')
OUTPUT
Unique lines from the input file are written to the output file.
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
What are arithmetic operators in Python?
Arithmetic operators are symbols used to perform mathematical operations: +, -, *, /, %, //, and **.
How do you remove duplicates from a file?
You can remove duplicates from a file by reading all lines, storing unique lines using a set or dictionary, and writing them to a new file.