7

As a beginner to Python(SAGE) I want to put the output of this for loop into a list:

for y in [0,8] : 
   for z in [0,1,2,3] :
     x=y+z

     print x

The output is

0
1
2
3
8
9
10
11

(vertically). I want a list so I can use it for a later operation: I want [1,2,3,8,9,10,11]. I found a similar question and I realize that the output is recalculated each time. Is there a simple way to store them in a list? Following a suggestion for the other answer, I tried "append" like this, but it gives an error message:

x=[]
for y in [0,2*3] : 
 for z in [0,1,2,3] :
   x=y+z
   x.append(x)

  print x
Laurel
  • 5,965
  • 14
  • 31
  • 57
Albert
  • 89
  • 1
  • 1
  • 3
  • You overwrote the name of the list. How is the interpreter supposed to know what you mean when you call everything `x`? – TigerhawkT3 Sep 05 '16 at 00:33
  • 2
    You're using the variable `x` for two different things: the list and the calculated number. Use it for only one of them and get an additional variable for the other. Like, `w = y + z` and then `x.append(w)`. – sparc_spread Sep 05 '16 at 00:33
  • What do you mean by **Python(SAGE)**? If SAGE is an acronym, then there are quite a few ways to interpret it: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=what+does+the+acronym+SAGE+mean. – Christian Dean Sep 05 '16 at 01:03
  • Thank you everyone very much for all your answers!!! I had just discovered something myself like sparc_spread's, and it works fine. But I will go through the other answers as well to learn something. It was really driving me crazy but that's how you learn! – Albert Sep 05 '16 at 04:57

7 Answers7

12

You have a lots of ways! First, as a raw code, you can do this:

lst=[]
for y in [0,8] : 
    for z in [0,1,2,3] :
        x=y+z
        lst.append(x)

print lst

You can try list comprehension:

lst = [y+z for y in [0,8] for z in [0,1,2,3]]
print lst

You can also use itertool chain:

import itertools  
lst = list(itertools.chain(*[range(i, i+4) for i in [0,8]]))
print lst

Another way is itertool products:

import itertools
lst = [y+z for y,z in list(itertools.product([0,8], [0,1,2,3]))]
print lst

In all cases, output is : [0, 1, 2, 3, 8, 9, 10, 11]

jbsu32
  • 1,036
  • 1
  • 11
  • 31
2

This should do the trick:

lst = [y+z for y in [0,8] for z in [0,1,2,3]]
print(lst) # prints:  [1,2,3,8,9,10,11]

The reason your code did not work, is because your using the variable x for two different things. you first assign it to a list, then you assign it to a integer. So python thinks that x is a integer, and integers don't have the attribute append(). Thus Python raise an error. The remedy is just to name your variables different things. But you should use something more descriptive than x, y, and z, unless their 'throwaway' variables.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

Because you set variable x from list to int at this line: x = y + z.

for-loops don't have scopes like a function or class, so if you set a variable out of the loop, the variable is the same as if it were in the loop.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
wang aaron
  • 19
  • 1
  • 1
  • 3
0

try this :

import itertools
temp = [y+z for y,z in list(itertools.product([0,8], [0,1,2,3]))]
florex
  • 943
  • 7
  • 9
0

You can also approach this functionally using itertools.product:

from itertools import product

lst = [y + z for y, z in product([0, 8], [0, 1, 2, 3])]
print(lst)

will output [0, 1, 2, 3, 8, 9, 10, 11].

  • he does not have to use `itertools.product`. He can simply say: `lst = [y+z for y in [0,8] for z in [0,1,2,3]]`. – Christian Dean Sep 05 '16 at 00:51
  • Fair enough, but I'm not really a fan of that syntax, almost feels like its gonna behave as `lst = [(y+z for y in [0,8]) for z in [0,1,2,3]]`. itertools.product is clearer imo and shouldn't make much of a difference performance wise. – Daan van der Kallen Sep 05 '16 at 00:57
  • My point was that the OP didn't have to import anything, he could just use the tools already in his files namespace. but to each his own I guess. – Christian Dean Sep 05 '16 at 00:59
0

Try:

x = [y+z for y in [0,8] for z in [0,1,2,3]]
acw1668
  • 40,144
  • 5
  • 22
  • 34
0

You can simply use a list comprehension such as this one:

>>> lst = [y+z for y in [0,8] for z in [0,1,2,3]]
>>> lst
[0, 1, 2, 3, 8, 9, 10, 11]

That's perfectly clear to any Python programmer.

Or you could use range:

>>> lst=[]
>>> for l in (range(i, i+4) for i in [0, 8]):
...     lst.extend(l)
>>> lst
[0, 1, 2, 3, 8, 9, 10, 11]

Or, since some seem to like complicated answers:

>>> import itertools  
>>> lst = list(itertools.chain(*[range(i, i+4) for i in [0,8]]))
>>> lst
[0, 1, 2, 3, 8, 9, 10, 11]
mhawke
  • 84,695
  • 9
  • 117
  • 138