16

My Python code generates a list everytime it loops:

list = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)

But I want to save each one - I need a list of lists right?

So I tried:

list[i] = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)

But Python now tells me that "list" is not defined. I'm not sure how I go about defining it. Also, is a list of lists the same as an array??

Thank you!

user1551817
  • 6,693
  • 22
  • 72
  • 109
  • Please show your entire loop, not just the line that performs the assignment. – David Robinson Sep 06 '12 at 05:00
  • 13
    it is a BAD idea to create a variable that is the same name as a builtin. – tMC Sep 06 '12 at 05:03
  • An array is not a list of lists. "array" has different meanings in different languages (in C an array cannot be resized, in Perl one can) but in most cases it is roughly analogous to a regular Python list. – David Robinson Sep 06 '12 at 05:03
  • 1
    Show an example of the file 'temp.txt', and explain what you want the result in python to look like. – Warren Weckesser Sep 06 '12 at 05:11
  • 1
    It is a BAD idea to create a language which permits willy-nilly variables to redefine builtins. Unless the aim of the language is to allow others to rewrite the language using the language because the language is poorly designed. –  Apr 24 '19 at 16:48

5 Answers5

52

You want to create an empty list, then append the created list to it. This will give you the list of lists. Example:

>>> l = []
>>> l.append([1,2,3])
>>> l.append([4,5,6])
>>> l
[[1, 2, 3], [4, 5, 6]]
Roger Allen
  • 2,262
  • 17
  • 29
  • That's good but I struggle to understand why the below has a different result: >>> l=[1,2,3] >>> l.append([4,5,6]) >>> l [1, 2, 3, [4, 5, 6]] – Mr Ed Jun 14 '18 at 05:43
  • Maybe this helps? >>> l=[[1,2,3]] >>> l.append([4,5,6]) >>> l [[1, 2, 3], [4, 5, 6]] – Roger Allen Jun 15 '18 at 03:55
  • @user222216 if you want to sum lists use + , otherwise what you did, is on position 0->1, on position 1->2, on position 2->3, and finally you put on position 3 whole list-> [4,5,6] – makkasi Sep 17 '19 at 06:20
9

Create your list before your loop, else it will be created at each loop.

>>> list1 = []
>>> for i in range(10) :
...   list1.append( range(i,10) )
...
>>> list1
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9], [6, 7, 8, 9], [7, 8, 9], [8, 9], [9]]
Zulu
  • 8,765
  • 9
  • 49
  • 56
8

Use append method, eg:

lst = []
line = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)
lst.append(line)
fabiocerqueira
  • 762
  • 4
  • 12
  • 2
    Thanks man. I only used "list" as an example, but I see that I should have used something else. So I got this working as you suggested. Now when I print list[0] or list[7] etc., I get the right answer. But when I try and print the whole thing with print list, I get a weird looking readout with the word "array" and "dtype" in it. – user1551817 Sep 06 '12 at 05:58
3

First of all do not use list as a variable name- that is a builtin function.

I'm not super clear of what you're asking (a little more context would help), but maybe this is helpful-

my_list = []
my_list.append(np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1))
my_list.append(np.genfromtxt('temp2.txt', usecols=3, dtype=[('floatname','float')], skip_header=1))

That will create a list (a type of mutable array in python) called my_list with the output of the np.getfromtext() method in the first 2 indexes.

The first can be referenced with my_list[0] and the second with my_list[1]

tMC
  • 18,105
  • 14
  • 62
  • 98
1

Just came across the same issue today...

In order to create a list of lists you will have firstly to store your data, array, or other type of variable into a list. Then, create a new empty list and append to it the lists that you just created. At the end you should end up with a list of lists:

list_1=data_1.tolist()
list_2=data_2.tolist()
listoflists = []
listoflists.append(list_1)
listoflists.append(list_2)
Klaudijus
  • 93
  • 1
  • 6