# List comprehension

#1

I am trying to do some practice work on List comprehension. How would I use List comprehension in the code below? I have tried and failed. x is a list of c coordinates and y is a list of corresponding y coordinates. There are only 3 coordinates Thanks

x=[]
for i in range (3): # 3 because we need three x coordinates
x.append (int(input())) # stores your x coordinates
y=[]
for j in range (3): # 3 because we need the three equivalent/corresponding y coordinates
y.append(int(input()))

#2
``````x = [i for i in range(3)]
y = [i for i in range(3)]
``````

#3
``````>>> c = [(a, b) for a in range(3) for b in range(3)]
>>> c
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
>>>
``````

#4

Thanks Roy that’s useful. I shall copy that and save it. The whole code is as shown below just to give you the whole flavour. It works. Ijust want to improve using list comprehension. I tried what you suggested butit did not work. Excuse the amateurishness. I intend to upload this together with the normal calculation if that’s OK with you

print(’’’ This is a code for calculating the area of a parallelogram given three coordinates’’’)
print (’’‘you may get a negative answer depending on whiich coords you choose first’’’)

enter then your third x cordinate. Then repeat for your y coordinates and then a final enter’’’)

x=[]
for i in range (3): # 3 because we need three x coordinates
x.append (int(input())) # stores your x coordinates
y=[]
for j in range (3): # 3 because we need the three equivalent/corresponding y coordinates
y.append(int(input())) # stores your y coordinates
x.append(x[0]) #appends the first x coordinate to the end
y.append(y[0]) #appends the first ycoordinate to the end
print (x)
print (y)
a=x[0]*y[1]+x[1]*y[2] +x[2]*y[3] #cross/diagonally multiplying x and y coordinate from left to right
print (a)
b =x[3]*y[2]+x[2]*y[1] +x[1]*y[0] #cross/diagonally multiplying x and y coordinate from right to left
print(b)
c= a - b
print(" area = “, c, " square units”)

#5

sorry these are my outputs

0
6
-7
-1
1
4
[0, 6, -7, 0]
[-1, 1, 4, -1]
31
-13
area = 44 square units