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.
CodePudding user response:
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.
CodePudding user response:
import csv
data = 'fruits.csv'
fruits_in = []
fruits_out = []
# read csv, append unique items to list
with open(data, 'r') as f:
reader = csv.reader(f)
for row in reader:
if row not in fruits_in:
fruits_in.append(row)
# flatten list of lists
for sublist in fruits_in:
for element in sublist:
fruits_out.append(element)
# output: ['row A', 'apple', 'orange', 'watermelon']
print(fruits_out)