First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

First, for n in len(S) won't work, because len(S) is an integer:

TypeError: 'int' object is not iterable

Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.

Next, to create an object for each element of S, you can use list comprehension:

sage: [i for i in S]  # or [(stuff depending on i)  for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)]  # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]
click to hide/show revision 2
No.2 Revision

First, for n in len(S) won't work, because len(S) is an integer:

TypeError: 'int' object is not iterable

Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.

Next, to create an object for each element of S, you can use list comprehension:

sage: [i for i in S]  # or [(stuff depending on i)  for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)]  # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]

Edit: you can also do this without list comprehension:

K = []
for i in S:
    K.append(stuff)
click to hide/show revision 3
No.3 Revision

First, for n in len(S) won't work, because len(S) is an integer:

TypeError: 'int' object is not iterable

Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.

Next, to create an object for each element of S, you can use list comprehension:

sage: [i for i in S]  # or [(stuff depending on i)  for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)]  # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]

Edit: you can also do this without list comprehension:

K = []
for i in S:
    (create stuff)
    K.append(stuff)
click to hide/show revision 4
No.4 Revision

First, for n in len(S) won't work, because len(S) is an integer:

TypeError: 'int' object is not iterable

Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.

Next, to create an object for each element of S, you can use list comprehension:

sage: [i for i in S]  # or [(stuff depending on i)  for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)]  # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]

Edit: you can also do this without list comprehension:

K = []
for i in S:
    (create stuff)
    K.append(stuff)

Then K will be a list with one entry for each element of S.