Lists and functions


#1

I have finished the basic codecademy exercise but I have forgotten almost everything I learnt so I am back. In the exercise below, I am trying to find the numbers common to both lists a and b. One can probably do this using the principles in Venn Diagram but I want to see if it can be done by list comprehension. In this instance my expression is x-y, (x minus y), because if you subtract two numbers of equal value the answer should be zero. In the code below, I am getting the error message
"There is an error (invalid syntax) at line 8" i.e., the last line. Please help. Two questions

  1. Is it possible to use the expression x - y?
  2. What is wrong with the last line?
    The numbers generated are random. I have included some numbers so as to repeat them e.g 5 and 21. Thank you

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

import random
a=[2, 5, 9, 10, 12, 13, 15, 18,5, 20, 21, 24, 25, 26, 28, 30]
b=[5, 7, 14, 15, 17, 21, 21,24, 25, 27, 28, 29, 30, 31]
print (a)
print (b)
c=[]
[x-y for x in a and y in b if x -y == 0 c.append (x)]

<do not remove the three backticks above>

#2

if you want to loop over two lists at the same time, you need zip(), you can’t just squaze in two loops.


#3

Thank you for that. Apart from the zip thingy, did the general thinking make any sense in terms of using the expression x - y. I was exploring the possibility of doing it in one line. I have never heard of zip…I shall do some search on that. Thanks friend


#4

you want to find elements which are presents in both lists? then zip() won’t do.

you should use a single loop (for example for x in a) and then as if condition check if x in b to see if number is present in list, if so, append x to c


#5

OK I shall try that as well. Meanwhile using zip, I had partial success as below
import random
a=[2, 5, 9, 10, 12, 13, 15, 18,5, 20, 21, 24, 25, 26, 28, 30]
b=[5, 7, 14, 15, 17, 21, 21,24, 25, 27, 28, 29, 30, 31]
print (a)
print (b)
print([item for item in a if item in b])
#print(
[item for item in b if item in a])

Note that using the penultimate line I got 5 15 5 21 24 25 28 30 with 5 repeating because there were two 5s in list a
Using the last line I got 5 15 21 21 24 25 28 30 with 21 repeating but not 5., two 21s in list b
Next, I shall try your method. Very interesting.Thanks


#6

depending on what this is for, you can also sort the list (using .sort() or write your own sorting algorithm) and then using a search algorithm, which would be interessting


#7

I was thinking sub sets.
Your suggestion also worked except that I had to remove […c,append(x)] and c=[]. I am not sure why That caused an obstruction.
thanks again

import random

a=[2, 5, 9, 10, 12, 13, 15, 18,5, 20, 21, 24, 25, 26, 28, 30]
b=[5, 7, 14, 15, 17, 21, 21,24, 25, 27, 28, 29, 30, 31]
print (a)
print (b)

#print([item for item in a if item in b])
#print(
[item for item in b if item in a])
c=[x for x in a if x in b ]
print©


#8

There are plenty of possibilties, depending on what you wand and the purpose of the program


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.