'How do I put a single csv row into one list with no duplicates?
I have a csv file with a row that I need to put into a list. An example of the row would be
row A
apple
apple
apple
orange
orange
watermelon
and i need to read that row into a list without the duplicate names, so it would look like
['apple','orange','watermelon']
Here is my current code for this problem:
import csv
start = open('fruits.csv', 'r')
reader = csv.reader(start)
next(reader, None)
for row in reader:
fruits = [row[1]]
print(fruits)
My current code just puts each individual line into its own list.
Solution 1:[1]
In the code you provided, you are creating a new list with a single item every time you loop though the for loop. Instead, you want to maintain a growing list
import csv
start = open('data.txt', 'r')
reader = csv.reader(start)
next(reader, None)
fruits = [] #define an empty list
for row in reader:
fruits.append(row[1]) #add to the list
Note that in the data example you provide, there is only one column so it should be row[0] instead of row[1] if we are strictly using that example
To make the list unique, you can convert it into a set which enforces uniqueness:
fruits = set(fruits)
If you want it to be converted back into a list, try the following:
fruits = list(fruits)
Note that this method does not guarantee the order of the list will stay the same.
Solution 2:[2]
import csv
data = 'fruits.csv'
fruits = []
# read csv, append unique items to list
with open(data, 'r') as f:
reader = csv.reader(f)
for row in reader:
if row[0] not in fruits:
fruits.append(row[0])
# output: ['row A', 'apple', 'orange', 'watermelon']
print(fruits_in)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ScottMastro |
| Solution 2 |
