Remove items from a list

Q

Quinn Dunkan

Egbert Bouwman said:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....
The answers you received don't tell you what you are doing wrong.
If you replace 'del each' with 'print each' it works,
so it seems that you can not delete elements of a list you are
looping over. But I would like to know more about it as well.
egbert

"for each in ..." makes 'each' signify an element of _dbases. Then
"del each" makes 'each' no longer signify anything. So the above doesn't
really do anything at all. "del _dbases[x]" however does work, but
notice that if you delete element 3, element 4 becomes element 3, etc. Then
when 'x' is incremented to 4, you've skipped what used to be element 4 (which
is now element 3). In general, modifying a list while iterating over it is
more trouble than it's worth. Go with the listcomp solutions.
 
D

Dan Bishop

Egbert Bouwman said:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....
The answers you received don't tell you what you are doing wrong.
If you replace 'del each' with 'print each' it works,
so it seems that you can not delete elements of a list you are
looping over. But I would like to know more about it as well.
egbert

"for each in ..." makes 'each' signify an element of _dbases. Then
"del each" makes 'each' no longer signify anything. So the above doesn't
really do anything at all. "del _dbases[x]" however does work, but
notice that if you delete element 3, element 4 becomes element 3, etc. Then
when 'x' is incremented to 4, you've skipped what used to be element 4 (which
is now element 3). In general, modifying a list while iterating over it is
more trouble than it's worth. Go with the listcomp solutions.

And if for some reason you can't use listcomps, you can use an approach like:

for (x, each) in enumerate(_dbases):
if each[-4:] != ".dbf":
# don't use del, just mark the slot as empty
_dbases[x] = None
# Now filter out Nones
_dbases = [x for x in _dbases if x is not None]
 
D

Duncan Booth

(e-mail address removed) (Peter Abel) wrote in
When you iterate over a list with a for-loop as you do it,
you get a copy of "each" item of the list. What you're doing
is deleting this copy, which is bound to the variable *each*.

This explanation is badly wrong.

None of the items in the list is copied, nor are any objects (copied or
otherwise) being deleted. A new reference is created to each of the items,
and that reference is deleted either by the 'del' statement, or when each
is rebound or goes out of scope.

The objects themselves are deleted only when the last reference to the
object is deleted (which doesn't happen here).
 
P

Peter Abel

Duncan Booth said:
(e-mail address removed) (Peter Abel) wrote in


This explanation is badly wrong.

None of the items in the list is copied, nor are any objects (copied or
otherwise) being deleted. A new reference is created to each of the items,
and that reference is deleted either by the 'del' statement, or when each
is rebound or goes out of scope.

The objects themselves are deleted only when the last reference to the
object is deleted (which doesn't happen here).

This explanation is correct.

My english is not yet good enough to enunciate in that brilliant way
you did but I promise I'll work hard on it (mea culpa) :)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top