Skip to content

Instantly share code, notes, and snippets.

@dideler
Last active April 8, 2024 04:17
Show Gist options
  • Save dideler/4688053 to your computer and use it in GitHub Desktop.
Save dideler/4688053 to your computer and use it in GitHub Desktop.
Removing duplicate lines from a file in Python
#!/usr/bin/python
"""
Playing around with slightly various ways to simulate uniq in Python.
The different strategies are timed.
Only m1() and m2() do not change the order of the data.
`in` is the input file, `out*` are output files.
"""
infile = 'in' # Change filename to suit your needs.
def m1():
s = set()
with open('out1', 'w') as out:
for line in open(infile):
if line not in s:
out.write(line)
s.add(line)
def m2():
s = set()
out = open('out2', 'w')
for line in open(infile):
if line not in s:
out.write(line)
s.add(line)
out.close()
def m3():
s = set()
for line in open(infile):
s.add(line)
out = open('out3', 'w')
for line in s:
out.write(line)
out.close()
def m4():
s = set()
for line in open(infile):
s.add(line)
out = open('out4', 'w').writelines(s)
def m5():
uniqlines = set(open(infile).readlines())
out = open('out5', 'w').writelines(uniqlines)
if __name__ == '__main__':
import timeit
print 'm1', timeit.timeit('m1()', setup='from __main__ import m1', number=1000)
print 'm2', timeit.timeit('m2()', setup='from __main__ import m2', number=1000)
print 'm3', timeit.timeit('m3()', setup='from __main__ import m3', number=1000)
print 'm4', timeit.timeit('m4()', setup='from __main__ import m4', number=1000)
print 'm5', timeit.timeit('m5()', setup='from __main__ import m5', number=1000)
@satyapendem
Copy link

thanks

@sagivn
Copy link

sagivn commented Jun 9, 2016

Awesome, thank you!

@IceflowRE
Copy link

IceflowRE commented Sep 15, 2016

@jseldess
m1 and m2 do not change the order ;) m0is just a typo.

@om-mahato
Copy link

om-mahato commented Jul 17, 2018

To make the script compatible with python 3.6 modify bottom codes

if __name__ == '__main__':
    import timeit
    print ('m1: '+ str(timeit.timeit('m1()', setup='from __main__ import m1', number=1000)))
    print ('m2: '+ str(timeit.timeit('m2()', setup='from __main__ import m2', number=1000)))
    print ('m3: '+ str(timeit.timeit('m3()', setup='from __main__ import m3', number=1000)))
    print ('m4: '+ str(timeit.timeit('m4()', setup='from __main__ import m4', number=1000)))
    print ('m5: '+ str(timeit.timeit('m5()', setup='from __main__ import m5', number=1000)))

@om-mahato
Copy link

Result:

m1: 18.488440779925337
m2: 18.840775916894255
m3: 17.286346907121
m4: 17.045031414935742
m5: 18.767793934837414

For a 75 Kb file

@nil-swap
Copy link

I am looking for single method and found 5 to deal with duplicate rows.......Thanks a lot..

@Steglinsky
Copy link

Thanks, m5 was very useful to me

@mateusza
Copy link

mateusza commented Oct 1, 2023

uniq only removes consecutive repetitions. It doesn't keep track of all processed lines, as you do in all of your implementations. (each one uses set()) Instead, uniq only remembers the last line, and removes multiple occurrences of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment