Removing matching items from a list?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 3 18:37:41 EDT 2013


On Sat, 03 Aug 2013 13:12:55 -0700, kevin4fong wrote:

> Basically, I'm trying to find out how to remove matching items from a
> list. But there doesn't seem to be any information on how to go about
> doing this specific function.

The documentation cannot possibly cover every single one of the infinite 
number of things somebody might want to do with Python. Programming is 
about figuring out how to do the things you want from the tools provided 
by the language.


> For example, what I want is:
> 
> let's say there is a list:
> 
> pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd']
> 
> So up there, my list, which is named pHands[0] has ten items in it.

Ten items? I wouldn't have known that if you didn't say so! *wink*

But seriously... you don't need to repeat obvious things like that. We 
can count as well as you can.

But more importantly, it is irrelevant. Is "ten items" important to this 
problem? No. What if there were nine, or thirty five, or seven? Would the 
problem of "removing matching items" change? No, the problem remains the 
same regardless of how many items there are.


> I'm trying to make a function where a search is initiated into the list
> and any matching items with a first matching number/letter reaching four
> are removed

In general, rather than removing items from a list, it is faster and 
safer (less error-prone) to create a new list with the items not removed. 
But either way, when trying to program, start by thinking about how *you* 
would do this task by hand:

1) I would start by counting how many items start with each initial 
letter: in your example I would count those starting with 'a' (4), 
'7' (1), and so on.

2) Then go through those initial letters, and pick out the ones equal to 
4 (or should that be "four or more"?). In this case, only 'a' is 4 or 
more, but there could be multiple examples, say, 4 'a's and 6 'b's.

3) Then, either delete those items from the original list, *or* make a 
new list with the remaining items. Making a new list is usually better.

Now start turning each step into Python code.

Step 1:

Iterate over each item, grab the first character of that item, and add 
one to a counter.

counts = {}  # Start with an empty dict.
for item in pHands[0]:
    c = item[0]  # Grab the first character.
    if c in counts:
        counts[c] += 1
    else:
        counts[c] = 1


This sets the counter to 1 the first time each character is seen, 
otherwise increments the counter by 1. There's a ready-made tool for 
doing that:

from collections import Counter
counts = Counter(item[0] for item in pHands[0])

but under the hood, it's essentially doing the same thing as I wrote 
above.


Steps 2 and 3 can be done together:

new_list = []
for item in pHand[0]:
    c = item[0]
    # Look up the character in the counter.
    if counts[c] < 4:
        new_list.append(item)

pHand[0] = new_list



-- 
Steven



More information about the Python-list mailing list