Skip to content
Advertisement

count of unique data in one column of list of lists

I have got list of lists, have to count number of unique values in one of the columns from that list. I have got only to the point to extract that one column from the list by using the following

j = 1
    for i in range(len(table)):
    row = table[i]
    print(row[j])

how can I now count unique data in that column?

Advertisement

Answer

Use set to find unique elements

column_index = 1  # Set which column you need
len(set(row[column_index] for row in table))

OR

columns = list(zip(*table))
len(set(columns[column_index])) # Number of unique items
sum(columns[column_index]) # Sum of column
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement