Skip to content Skip to sidebar Skip to footer

Storing Values In A Csv File Into A List In Python

I'd like to create a list that stores all values of a single column. For example let's say my file has a column called 'FirstNames' and for the first 3 rows, the 'names' column ha

Solution 1:

Please never use reserved words like list, type, id... as variables because masking built-in functions.

If later in code use list e.g.

list = data['FirstNames'].tolist()
#another solution for converting to listlist1 = list(data['SecondNames'])

get very weird errors and debug is very complicated.

So need:

L = data['FirstNames'].tolist()

Or:

L = list(data['FirstNames'])

Also can check Is it safe to use the python word “type” in my code.

Solution 2:

Because you are using reserved variable names. Instead of using list, use list1 or something else as the variable name.

Few of the reserved variable names in python are: dict, str, list, int, pass etc, which we use mistakenly. Try to avoid it.

Post a Comment for "Storing Values In A Csv File Into A List In Python"