0

How can i compare 2 different list with their same positions only in python

Let we have two list a =[] and b = [] we compare it with their same position It means a[0] with b[0] a[1] with b[1] a[2] with b[2]

3rd Jul 2019, 2:48 PM
Zinn Kazama
Zinn Kazama - avatar
9 ответов
+ 3
Ok, well I think the simplest way would be to do something like this: for i in range(len(a)): if a[i] > b[i]: {some code to print what you need} elif a[i] < b[i]: {more code} else: {they are the same} Note that, while you are dealing with strings and not numbers, '7' will return as greater than '10'.
4th Jul 2019, 6:54 AM
Russ
Russ - avatar
+ 2
Hope this is what you are looking for - https://code.sololearn.com/cMYdem5mpe78/?ref=app
3rd Jul 2019, 4:10 PM
Kuri
Kuri - avatar
+ 1
Hi... Comparing lists as in above code is sufficient. So u see any issues with code? If(a==b)....
4th Jul 2019, 4:55 AM
Kuri
Kuri - avatar
+ 1
Mr. Russ you note line helped me thank you
4th Jul 2019, 7:40 AM
Zinn Kazama
Zinn Kazama - avatar
0
What comparisons do you want to do exactly? My feeling is you will need the zip() method. This is a generator function that returns tuple after tuple of each list's fist element, followed their second element and so on. Example: a = [1,2,3] b = [4,5,6] print(*[i for i in zip(a,b)]) # (1, 4) (2, 5) (3, 6) If you want to compare if each elements in one list is equal to its corresponding element in the other: a = [1,2,3] b = [1,2,3] print(all(x == y for (x,y) in zip(a,b))) # True
3rd Jul 2019, 3:36 PM
Russ
Russ - avatar
0
I already tried these all but it is not helping me
3rd Jul 2019, 6:45 PM
Zinn Kazama
Zinn Kazama - avatar
0
Zinn Kazama then you need to explain how you are trying to compare them. What lists do you have? In what way are you comparing them? What result are you expecting?
3rd Jul 2019, 7:23 PM
Russ
Russ - avatar
0
Russ Let i have two list in first a =[ '5','6','7'] and in second list b = ['3','6','10'] given element. Now i have to compare the both list's elements with their same postion not a list with another list for example a[0] with b[0] if a[0] < or > b[0] then i got my value similarly i have to compare a[1] with b[1] and a[2] with b[2] only and sorry i didnt explain correctly before and thank you
4th Jul 2019, 4:34 AM
Zinn Kazama
Zinn Kazama - avatar
0
this code is only comparing two list and giving result that those two lists are identical or not
4th Jul 2019, 4:58 AM
Zinn Kazama
Zinn Kazama - avatar