Sep-05-2021, 06:44 PM
If the file is already sorted on the first column, you can invoke
itertools.groupby()
# untested code but you get the idea import csv import itertools as itt from operator import itemgetter def unique_rows(rows): for key, group in itt.groupby(rows, key=itemgetter(0)): yield next(group) def main(): with open('input.csv') as ifh, open('output.csv', 'w') as ofh: rd = csv.reader(ifh) wt = csv.writer(ofh) wt.writerows(unique_rows(rd)) if __name__ == '__main__': main()