0

I want to make a list of lists in python.

My code is below.

import csv
f = open('agGDPpct.csv','r')
inputfile = csv.DictReader(f)

list = []

next(f) ##Skip first line (column headers)

for line in f:
    array = line.rstrip().split(",")
    list.append(array[1])
    list.append(array[0])
    list.append(array[53])
    list.append(array[54])
    list.append(array[55])
    list.append(array[56])
    list.append(array[57])
print list

I'm pulling only select columns from every row. My code pops this all into one list, as such:

['ABW', 'Aruba', '0.506252445', '0.498384331', '0.512418427', '', '', 'AND', 'Andorra', '', '', '', '', '', 'AFG', 'Afghanistan', '30.20560247', '27.09154001', '24.50744042', '24.60324707', '23.96716227'...]

But what I want is a list in which each row is its own list: [[a,b,c][d,e,f][g,h,i]...] Any tips?

4
  • 2
    list.append([array[1],array[0],array[53], etc]) Commented May 6, 2016 at 12:35
  • 1
    As a side note, you probably want to use the csv module. Here you import it and create a DictReader() object, but then proceed to read directly from your file handle and manually split on commas. Commented May 6, 2016 at 12:37
  • Typing in your title into a random search engine gave me this: mail.python.org/pipermail/tutor/2000-January/000946.html Commented May 6, 2016 at 12:37
  • Thanks! @Tim works great √+ Commented May 7, 2016 at 7:03

4 Answers 4

2

You are almost there. Make all your desired inputs into a list before appending. Try this:

import csv 
with open('agGDPpct.csv','r') as f:
  inputfile = csv.DictReader(f)
  list = []

  for line in inputfile: 
    list.append([line[1], line[0], line[53], line[54], line[55], line[56], line[57]])
print list
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I realized I'm screwing up the reader code. But when I try your suggestion I get a Key Error:1 seemingly related to the .append line.
I figured out the problem: Key Error wants the default fieldnames from the first row (e.g. "first name", "last name", etc.), not the column numbers 1, 2, etc. I figured that much but it still wouldn't work. The problem seems to be the "next(f)". Remove this and the code works. Maybe it was skipping the fieldnames, not sure.
That absolutely was the problem. You used next(f) when not using the csv class, using the csv class obviates the need to use next(f) since when you declare a DictReader, it uses the first row as header names. Good job figuring that out.
0

To end up with a list of lists, you have to make the inner lists with the columns from each row that you want, and then append that list to the outer one. Something like:

for line in f:
    array = line.rstrip().split(",")
    inner = []
    inner.append(array[1])
    # ...
    inner.append(array[57])
    list.append(inner)

Note that it's also not a good practice to use the name of the type ("list") as a variable name -- this is called "shadowing", and it means that if you later try to call list(...) to convert something to a list, you'll get an error because you're trying to call a particular instance of a list, not the list built-in.

Comments

0

To build on csv module capabilities, I'll do

import csv
f = csv.reader(open('your.csv'))
next(f)

list_of_lists = [items[1::-1]+items[53:58] for items in f]

Note that

  • items is a list of items, thanks to the intervention of a csv.reader() object;
  • using slice addressing returns sublists taken from items, so that the + operator in this context means concatenation of lists
  • the first slice expression 1::-1means from 1 go to the beginning moving backwards, or [items[1], items[0]].

1 Comment

Interesting, my first encounter with this code but it works perfectly. Thanks!
0
  1. Referring to https://docs.python.org/2/library/csv.html#csv.DictReader

Instead of

for line in f:

Write

for line in inputfile:
  1. And also use list.append([array[1],array[0],array[53],..]) to append a list to a list.

  2. One more thing, referring to https://docs.python.org/2/library/stdtypes.html#iterator.next , use inputfile.next() instead of next(f) .

After these changes, you get:

import csv
f = open('agGDPpct.csv','r')
inputfile = csv.DictReader(f)

list = []

inputfile.next() ##Skip first line (column headers)  

for line in inputfile:
    list.append([array[1],array[0],array[53],array[54],array[55],array[56],array[57]])
print list

In addition to that, it is not a good practice to use list as a variable name as it is a reserved word for the data structure of the same name. Rename that too.

You can further improve the above code using with . I will leave that to you.

Try and see if it works.

4 Comments

Why? Reader object has been stored in inputfile I suppose? See docs.python.org/2/library/csv.html#csv.DictReader
You changed your answer quite a bit.. anyway the reader already splits by comma, you don't have to do that manually
Thanks @TimCastelijns ! Edited the answer. And I still maintain that inputfile will work in for loop and not f :)
Thanks for this. When I try it it says 'array' is undefined however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.