Hanan
Hanan

Reputation: 1179

if statement against each item in a list

I am trying to do something like the following in python, but i am getting an invalid syntax error:

network_n = 192.168.38
octate_n = network_n.split(".")
if (len(o) [for o in octate_n]) == 3:

But when i am run it, i get:

if (len(o) [for o in octate_n]) == 3:
                  ^
SyntaxError: invalid syntax

My problem is with the if statement, what is wrong there ?

I admit that i have a rough idea about the difference between the () and the [] in the if statement, so i would be glad that you give me a short explanation about it.

Upvotes: 0

Views: 218

Answers (4)

Peter Downs
Peter Downs

Reputation: 482

I'm not sure exactly what your question is. Here are a bunch of possibilities:

If you want to return all the members of octate_n that have a length of 3:

with_len_3 = [o for o in octate_n if len(o) == 3]

If you want to make sure there are 3 elements in octate_n:

if len([o for o in octate_n]) == 3:
    # do stuff

which is the same as

if len(octate_n) == 3:
    # do stuff

If you want to make sure that all of the elements in octate_n are of length 3:

if( all(len(o) == 3 for o in octate_n) ):
    # do stuff

this is a cleaner, faster version of:

if len([o for o in octate_n if len(o) == 3]) == len(octate_n):
    # do stuff

Upvotes: 0

Björn Pollex
Björn Pollex

Reputation: 76778

Use all:

if(all(len(o) == 3 for o in octate_n)):
    # do stuff

About your seconds question, [o for o in octate_n] creates a list that consists of all items in octate_n (this is called a list comprehension), while (o for o in octate_n) creates a generator that will yield all the items of octate_n (this is called a generator expression).

Upvotes: 6

David Z
David Z

Reputation: 131550

Well, I can tell you what's wrong with the syntax of your conditional: you at least need to include len(o) inside the brackets.

[len(o) for o in octate_n]

This structure, [expr for var in list], is called a list comprehension and it's a shorthand for "evaluate expr for each element of list, with var set to the element, and make a new list of the results." In your original code you didn't have any expr which is why Python was complaining.

Now, even if you fix that, you wind up with

if [len(o) for o in octate_n] == 3:

which still has the problem that you are comparing a list to a number. While you can technically do that, it probably doesn't mean what you expect: a list will never be equal to a number, because they are different objects. It's not quite clear what you were trying to do, though. If you wanted to find whether all the elements of the list have length equal to 3, you would write

if all([len(o) == 3 for o in octate_n]):

or, more efficiently,

if all(len(o) == 3 for o in octate_n):

Note the absence of the square brackets in the later statement. This is because you don't actually need to make a list out of the results; you just need to be able to go through them one at a time and check if they're all true. Omitting the brackets is what indicates this to Python. (You can only do this inside parentheses.)

Upvotes: 2

Nikolay Fominyh
Nikolay Fominyh

Reputation: 9226

You using len on single element of the list. Emm.. no.. you use len(o) on element that is not defined. Use:

if (len(octate_n) == 3:

Sorry, got your idea. If you want to check that all of the elements of the list are 3 chars long - use "all" as in answer below.

Upvotes: 0

Related Questions