Kees
Kees

Reputation: 471

List of lists (not just list) in Python

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?

Upvotes: 0

Views: 124

Answers (4)

kingledion
kingledion

Reputation: 2500

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

Upvotes: 2

akshaynagpal
akshaynagpal

Reputation: 3147

  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.

Upvotes: 0

gboffi
gboffi

Reputation: 25023

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]].

Upvotes: 0

dcrosta
dcrosta

Reputation: 26258

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.

Upvotes: 0

Related Questions