473,238 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
4
by: wageslave | last post by:
Hi folks, I have a question about using consequtive combo boxes on a form. I am designing a basic library database for a small community organisation which deals with inner city problems and...
8
by: karokat | last post by:
Hi, I want to learn how to program and python seems to be the most intuitive language according to various internet sources - but I'm not sure if it's best for newbies... please advise.. Anyway,...
3
by: donchoi | last post by:
Hi, newbie here, sorry. I have a couple of basic Python questions. 1) How do I open 'datafile' and skip to the ith line for reading? I have a loop counter i for this purpose. The data file is very...
259
by: user923005 | last post by:
It would be really nice if C could adopt a really nice algorithms library like C++'s STL + BOOST. The recent "reverse the words in this sentence" problem posted made me think about it. It's...
2
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include...
2
by: clouddragon | last post by:
Hi, i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
8
by: jch | last post by:
Sorry for the newbie question but I'm trying to learn Visual Studio. I've got VS Express 2008 and I'm using visual basic. I'm trying to learn how to deploy a program so I've written a very basic...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.