473,238 Members | 1,745 Online

# Newbie needs help on using for and lists

I'm really really newbie, almost no knowledge about python.

The problem I have is :
example)
Expand|Select|Wrap|Line Numbers
1.  a = [ 1, 2, 3, 4, 5, 6, 7 ]
2.  for n in a:
3.      if a[n] < 4:
4.         del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
Aug 21 '07 #1
6 1046
Rather than deleting items that fail the test, try copying the good'uns to a new list.
Aug 21 '07 #2
ilikepython
844 Expert 512MB
I'm really really newbie, almost no knowledge about python.

The problem I have is :
example)
Expand|Select|Wrap|Line Numbers
1.  a = [ 1, 2, 3, 4, 5, 6, 7 ]
2.  for n in a:
3.      if a[n] < 4:
4.         del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
I don't get what you are trying to do. You have a list of indexes (??) that you use to acces that same list? Is this what you want:
Expand|Select|Wrap|Line Numbers
1. a = range(1, 8) # generates a list like yours
2. new = []
3. for n in a:
4.     if n < 4:
5.         new.append(n)
6.
Or you could use filter():
Expand|Select|Wrap|Line Numbers
1. a = range(1, 8)
2. new = filter(lambda x: x < 4, a)
3.
Or maybe a list comprehension is clearer:
Expand|Select|Wrap|Line Numbers
1. a = range(1, 8)
2. new = [n for n in a if n < 4]
3.
Aug 21 '07 #3
bartonc
6,596 Expert 4TB
I'm really really newbie, almost no knowledge about python.

The problem I have is :
example)
Expand|Select|Wrap|Line Numbers
1.  a = [ 1, 2, 3, 4, 5, 6, 7 ]
2.  for n in a:
3.      if a[n] < 4:
4.         del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
Our new friend, Strider1066, has a good suggestion.
For a quick copy of a simple list, use a "slice":
Expand|Select|Wrap|Line Numbers
1. >>> for i, item in enumerate(a[:]):  # A "slice" from beginning to end
2. ...     if item < 4:
3. ...         del a[i]
4. ...
5. >>> a
6. [2, 4, 6, 7]
7. >>>
Aug 21 '07 #4
ilikepython
844 Expert 512MB
Our new friend, Strider1066, has a good suggestion.
For a quick copy of a simple list, use a "slice":
Expand|Select|Wrap|Line Numbers
1. >>> for i, item in enumerate(a[:]):  # A "slice" from beginning to end
2. ...     if item < 4:
3. ...         del a[i]
4. ...
5. >>> a
6. [2, 4, 6, 7]
7. >>>
Are you sure that works? I mean, 2 is not greater than 4. It works with a.remove() but I get the same results like yours when I use del. I never actually use del. What does it do?
Aug 21 '07 #5
bartonc
6,596 Expert 4TB
Are you sure that works? I mean, 2 is not greater than 4. It works with a.remove() but I get the same results like yours when I use del. I never actually use del. What does it do?
OOoooops! Can't rely on the index into a changing list.
Remove works because it doesn't rely on the index of the item.
Expand|Select|Wrap|Line Numbers
1. >>> a = [ 1, 2, 3, 4, 5, 6, 7 ]
2. >>> for item in a[:]:  # A "slice" from beginning to end
3. ...     if item < 4:
4. ...         a.remove(item)
5. ...
6. >>> a
7. [4, 5, 6, 7]
8. >>> a = [ 1, 2, 3, 4, 5, 6, 7 ]
9. >>> for item in a[:]:  # A "slice" from beginning to end
10. ...     if item < 4 or item == 7:
11. ...         a.remove(item)
12. ...
13. >>> a
14. [4, 5, 6]
15. >>>
Nice catch, my friend. Thank you.
Aug 21 '07 #6
Thank you everyone, it was wiser to copy and replace lists.

I'm going to like here. :) thx
Aug 21 '07 #7