nulltron
nulltron

Reputation: 646

Is it possible to slice through a list for an if condition

I've tried to find an example of something like this taking place. But I have yet to actually find out if it's possible. From reading list documentation i can iterate through a list by doing the following _FILTER[0:48].

Is there a way to make a statement like the one below work?

_FILTER = ['filter','filter2','etc']

for link in links:
    if link != _FILTER[0:48]
        do_something

I want the _FILTER[0:48] to be treated as an and, what I mean by that is I need all of the _FILTER options. e.g.

if link != _FILTER[0] and if link != _FILTER[1]..._FILTER[48]

Upvotes: 1

Views: 89

Answers (2)

Adam Fatka
Adam Fatka

Reputation: 29

It depends on what 'links' is, in this case...

if links is a iterable of list and you are looking to see if one of those lists matches your slice. Yes, it is possible. ex:

filter_list = ['1', '2', '3', '4', '5']
link_list = [['2', '3', '4'], ['1', '2'] ['3', '4', '5']]

for link in link_list:
   if link != filter_list[0:2]:
       print('no match, do stuff')

This will 'do stuff' on the first and last index of 'link_list' but list_list[1] does equal the filter_list slice and will not 'do stuff'.

If you are just trying to do stuff if the link index is not in the filter_list I would use the 'not in' keywords instead.

Upvotes: -1

gkusner
gkusner

Reputation: 1244

This is the general case:

_FILTER = ['filter','filter2','etc']

for link in links:
    if link not in _FILTER:
        do_something

Upvotes: 2

Related Questions