Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list in Python with the unique values of a CSV file?

I have CSV file that looks like the following,

1994, Category1, Something Happened 1
1994, Category2, Something Happened 2
1995, Category1, Something Happened 3
1996, Category3, Something Happened 4
1998, Category2, Something Happened 5

I want to create two lists,

Category = [Category1, Category2, Category3]

and

Year = [1994, 1995, 1996, 1998]

I want to omit the duplicates in the column. I am reading the file as following,

DataCaptured = csv.reader(DataFile, delimiter=',')  
DataCaptured.next()

and Looping through,

   for Column in DataCaptured:
like image 585
Gravity M Avatar asked Jun 26 '14 23:06

Gravity M


2 Answers

dawg pointed out one of the greatest tricks in Python: using set() to remove duplicates from a list. dawg shows how to build the unique list from scratch by adding each item to a set, which is perfect. But here's another equivalent way to do it, generating a list with duplicates and a list without duplicates using a list(set()) approach:

import csv

in_str = [
    'year, category, event',
    '1994, Category1, Something Happened 1',
    '1994, Category2, Something Happened 2',
    '1995, Category1, Something Happened 3',
    '1996, Category3, Something Happened 4',
    '1998, Category2, Something Happened 5'
    ]

cdr = csv.DictReader(in_str, skipinitialspace=True)

col = []
for i in cdr:
    col.append(i['category'])

# all items in the column...
print(col)
# only unique items in the column...
print(list(set(col)))
like image 78
rob_7cc Avatar answered Sep 28 '22 02:09

rob_7cc


You can do:

DataCaptured = csv.reader(DataFile, delimiter=',', skipinitialspace=True) 

Category, Year = [], []
for row in DataCaptured:
    if row[0] not in Year:
        Year.append(row[0])
    if row[1] not in Category:
        Category.append(row[1])    

print Category, Year        
# ['Category1', 'Category2', 'Category3'] ['1994', '1995', '1996', '1998']

As stated in the comments, if order does not matter, using a set would be easier and faster:

Category, Year = set(), set()
for row in DataCaptured:
    Year.add(row[0])
    Category.add(row[1])
like image 35
dawg Avatar answered Sep 28 '22 02:09

dawg