473,411 Members | 1,982 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,411 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 1052
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.