954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

deleting or popping from a list if a str within the list meets certain criteria?

if I wanted to remove all of the strings within a given list that met a certain criteria, such as having the str'a' in them, how would I go about doing that? the only way that's intuitive to me a for/in loop, but that can't be done. any ideas? Using Python3.2

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

The common idiom is

thelist[:] = (x for x in thelist if not criteria(x))

# for example

thelist[:] = (x for x in thelist if not 'a' in x)
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

never mind, I figured it out. Here's the function I was working on, suggestions on simplification?

def front_x(words):
    copy=words[:]
    for i in copy:
        if i[0]!= str('x'):
            copy.remove(i)
            copy=sorted(copy, reverse=True)
    for i in words:
        if i[0]==str('x'):
            words.remove(i)
            words=sorted(words)
    print(copy+words)
pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

The common idiom is

thelist[:] = (x for x in thelist if not criteria(x))

# for example

thelist[:] = (x for x in thelist if not 'a' in x)


so it would be that simple to exclude those from the list?

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

and more specifically, I was working on if it wasn't the first character, but I think I'll add that idiom to my personal notes. Thank you!

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

The common idiom is

thelist[:] = (x for x in thelist if not criteria(x))

# for example

thelist[:] = (x for x in thelist if not 'a' in x)


Python has some extraordinarily intuitive features I'm noticing, it's almost...English.

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 
Python has some extraordinarily intuitive features I'm noticing, it's almost...English.


List comprehensions as Gribouillis use is a common way to write with python.

>>> l = ['a', 'b', 'c']
>>> a = [x for x in l if not 'a' in x]
>>> a
['b', 'c']
>>> l
['a', 'b', 'c']
>>>

If we break up List comprehensions it look like this.

>>> lst = []
>>> for x in l:
...     if not 'a' in x:
...         lst.append(x)
...         
>>> lst
['b', 'c']
>>> l
['a', 'b', 'c']
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Here's my view -

def front_x(words):

What does this name mean? I have no idea what this method does - I should be able to tell just by the name. My guess is that it moves the words with "x" at the start of the word to the front of the list, but I can't be sure.

copy=words[:]
    for i in copy:

i is generally used for numbers. Why not use "word"?

if i[0]!= str('x'):

Now, I don't know about you, but Python knows that 'x' is a string! :P

copy.remove(i)
            copy=sorted(copy, reverse=True)

Urgh. Please don't edit your iterables while you're using them to iterate. How about using using a different variable, like one named "result"?

for i in words:
        if i[0]==str('x'):
            words.remove(i)
            words=sorted(words)

Same applies here - and what's more, is that you can use result here!

print(copy+words)

Methods should do one of two things - edit a state (in a class) or return something. This does neither - how about returning the result, so you can use it in future?

If I were going to do this so that it was more readable and maintainable, I would do something like -

def move_words_starting_with_x_to_front(words):
    words = sorted(words)
    result = []
    for word in words:
	if word[0]=='x':
	    result.append(word)
    else:
	result.extend([word for word in words if word not in result])
    return result


Obviously, not the shortest, nor does it have the shortest method name, but you sure know what it's going to do and how ;) I would personally extend it again, making it work for any character, or multiple characters, or directly on a sentence.

Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
 

@Enilicho;the method name was made by google, it's google's python class. And lol I just noticed that I said str for 'x', a bit redundant, I've never programmed with any other language before but pytony seems to think I do types too much which makes me look like a java coder. Were you able to append to result because it wasn't the list the loop was about?

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

Ah, I see. I still think it's a poor name - I had no idea what it did until I read the code and saw the output. Very annoying when it comes to a big project ;)

Thinking about types is fine - you should know what type an object is, if you don't, then debugging becomes an issue.

And yup, the reason why I could edit result without worrying is because it wasn't the object I was iterating through.

Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: