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

Recommended Answers

All 9 Replies

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)
commented: Thank you so much! +1

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)

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?

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!

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.

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']
>>>
Member Avatar for Enalicho

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.

@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?

Member Avatar for Enalicho

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.