# Revision history [back]

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')]


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)


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)


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.