1

I have a Python list:

listx = [["a", 127, "Blue", 0],
         ["b", 127, "Red", 1],
         ["b", 127, "Green", 0],
         ["b", 99, "Green", 1],
         ["c", 99, "Yellow", 0]]

This is table view for understanding the situation better way:

This is table view

I want to do some filter function. For example; I want to get a list with index 0 = "b" and index 1 = 127. So the results must be:

listxnew = [["b", 127, "Red", 1],
            ["b", 127, "Green", 0]]

Table view for listxnew to understand the situation better way:

enter image description here

So how can I do this with simple Python code? Thanks.

3 Answers 3

4

You can do it using list comprehension as follows:

listxnew = [i for i in listx if i[0:2]==['b', 127]]

>>> print listxnew
[['b', 127, 'Red', 1]
 ['b', 127, 'Green', 0]]
Sign up to request clarification or add additional context in comments.

5 Comments

This is not a way that I want. Any def, function, or something like that? I will use this for a GUI. So, things can be filtered may be more than two, three or just one.
@user3051668, I'm not sure exactly what you mean but you can easily make a def out of the code above. Sorry
@user3051668 - Check if my answer works? I'm curious whether that's what you're looking for.
It works, but I don't want to write different codes for different filterings. If I want to filter with index 0 and index 2, it'll be different list comprehension.
@user3051668 You could readily put the list comprehension in a function and have it based on certain inputs (such as the indices you want to check and their desired value)
1

Here's an easy solution, which is readily extensible:

def filterls(ls, opts):
    """
    ls - list
    opts - dict - {id: match_info}
    """
    results = []
    for l in ls:
        for (i, t) in opts.items():
            if l[i] != t:
                break
        else:
            results.append(l)
    return results

For your example:

listx = [["a", 127, "Blue", 0],
         ["b", 127, "Red", 1],
         ["b", 127, "Green", 0],
         ["b", 99, "Green", 1],
         ["c", 99, "Yellow", 0]]

print filterls(listx, {0: 'b', 1: 127})
# [['b', 127, 'Red', 1], ['b', 127, 'Green', 0]]

2 Comments

I think this is only for Python 2.7. I got this with Python 3.4: AttributeError: 'dict' object has no attribute 'viewitems'
@user3051668 - Yes, in python 3.4, viewitems is just items. Edited.
1
listx = [["a", 127, "Blue", 0],
         ["b", 127, "Red", 1],
         ["b", 127, "Green", 0],
         ["b", 99, "Green", 1],
         ["c", 99, "Yellow", 0]]

listnew = filter(lambda x: x[0]=='b' and x[1]==127,listx)

print listnew

try using Filter

4 Comments

print listnew gives me <filter object at 0x0345DCF0>, am i missing something?
@user3051668 no it prints filtered list
@sundarnatarajСундар OP is using Python 3.x, I think.
@user3051668: just put list(listnew) to see what filter is producing under Python 3.x. +1 -- this is the best way IMHO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.