Python for an item in a list A AND listB

This question may be very simple and obvious to some people, but for some reason I could not find the answer on the Internet. I did not find my answer, having worked with IDLE and trying to understand how this works. How does a for loop work when multiple elements are specified?

a = [1,2,3,4,5] b = [6,7,8,9,0] for item in a and b: print 'why does this setup give the list b but nothing from a?' 

Questions for follow-up questions:

1) What can happen to other operators, such as or not?

2) Is this the right use, even? If so, is it dirty, insecure, or frowned?

+4
source share
6 answers

So you have two lists:

 >>> a = [1,2,3,4,5] >>> b = [6,7,8,9,0] 

... and you want to iterate over a and b . So what is a and b , exactly?

 >>> a and b [6, 7, 8, 9, 0] 

This may seem strange, but it is the result of two facts about Python:

  • Each object has either True -ish or False -ish. For instance:

     >>> bool(a) True >>> bool(b) True 

    In fact, all lists except the empty list [] are True -ish.

  • Python uses a short circuit estimate , which means for a and b this is:

    • Checks if there is a True -ish or False -ish

    • If a is False -ish, then it is equal to a

    • If a is True -ish, then b

    Following these rules, you should be able to see why a and b evaluates [6, 7, 8, 9, 0] in your case (and by the same rules for combinations of actual values, True and False will show you that evaluating the scheme makes sense).

If what you really want to do is iterate over the elements in a and then in b , you can simply use the + operator to concatenate them:

 >>> for item in a + b: ... print item, ... 1 2 3 4 5 6 7 8 9 0 

Regarding your follow up questions:

What could happen to other operators like or and not ?

or rules for evaluating a short circuit are different (you can view them yourself or just follow the link above), and in your case a or b is evaluated as [1, 2, 3, 4, 5] (in other words, a ).

not always returns True for the value False -ish and False for the value True -ish, and since you cannot TypeError over True or False TypeError .

Is this the right use, even? If so, is it dirty, insecure, or frowned?

Well, there is nothing illegal about it, but as you can see, it does not do what you want. There are circumstances in which (ab) using a short circuit estimate to select an iteration over which iteration can be performed, but this is not one of them.

+5
source

As you have discovered, for loops do not work when multiple elements are specified! What you get is an iteration over a and b. a and b returns something True if both elements are true; in this case, it is the rightmost operand, since it knows about it. The correct way to do this is itertools.chain:

  for item in itertools.chain(a, b): print 'now we get both lists' 
+4
source

This seems to be a situation where the use of Pythons in English ends in confusion. The for item in a and b operator does not mean "iterate over all the elements in a , and then all the elements in b " but rater "iterates over all the elements in the list that you get when applying and operations in lists a and b ".

So what you do is the last. You go through two lists a and b , and then iterate over the result. In the interpreter, both a and b you get

 >>> a = [1,2,3,4,5] >>> b = [6,7,8,9,0] >>> a and b [6, 7, 8, 9, 0] 

You can, of course, do this for any operations, but it still applies before the for loop.

To cycle through both lists one by one, replace the and operation with + to put them together, merge them. Like this

 >>> a + b [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> for item in a + b: ... print item, ... 1 2 3 4 5 6 7 8 9 0 
+1
source

The code a and b does not join the two lists; it performs a logical operation and an operation. Boolean and two true values, such as lists, evaluate the value of the second argument. If you use the or operator, it evaluates the first invalid value. Using not will evaluate to True for the invalid value and False for the true value. Usually, using the value of a and or or as nothing but a boolean is disapproving.

There are several ways to do what you are trying to do. The simplest would be to iterate over a + b - this will give you a new list. If your lists are large, you can use itertools.chain () , which allows you to iterate over all values ​​without creating a new list object.

+1
source

a and b will give b if both a and b are not None or empty or 0 . Try introducing 1 and 2 into the Python shell.

Try:

 a = [1,2,3,4,5] b = [6,7,8,9,0] for item in a + b: print item 

You can combine lists with the + operator.

0
source

You can also do:

 for i in a + b: print i 

This will merge the two lists and allow you to iterate.

0
source

Source: https://habr.com/ru/post/1491218/


All Articles