• Register
1 vote
20 views

Problem:

I was trying to find common elements in two lists python. So I wrote this code:
x = [1, 2, 6, 9]
y = [1, 3, 8, 9]


x_as_set = set(y)
intersection =x_as_set.intersection(y)


intersection_as_list = list(intersection)

print(intersection_as_list)

But when I compile this it gives error. Please help me to find common elements in two lists python

10 7 2
6,050 points

1 Answer

1 vote

Answer:

Main error of your code is caused by the line:

x_as_set = set(y)

If you put a list of ain a list of b it will not compare but it will take each of its elements to show.

So your code should be like:

 

x = [1, 2, 6, 9]
y = [1, 3, 8, 9]
a_as_set = set(a)
intersection =x_as_set.intersection(y)
intersection_as_list = list(intersection)
print(intersection_as_list)

And its output will be:

[1, 9]

 Happy coding.

11 6 4
34,930 points

Related questions

1 vote
2 answers 48 views
48 views
Problem: I am new to programming language. I was trying to find common elements in two lists using python. So I wrote this code: a = [1, 2, 6, 9] b = [1, 3, 8, 9] a_as_set = set(b) intersection =a_as_set.intersection(b) intersection_as_list = list(intersection) print(intersection_as_list) This code gives me the output: [8, 1, 3, 9] Which is not right. Please help me to get the exact output.
asked Mar 28 LizzyM 6.1k points
1 vote
1 answer 14 views
14 views
Problem: Hello kodlogs, I know the core python but when I have to implement the list concept with this that applying with two lists don't know how to implement this having issue, basically, I come from the c background and in c language there was not any concept of this ... I can see the content of the list is same still I am getting the error dont know how to achieve this waiting for the reply.
asked Jul 1 Gavin 15.3k points
1 vote
1 answer 21 views
21 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn’t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3 Gavin 15.3k points
1 vote
1 answer 24 views
24 views
Problem: Hello Kodlogs, I am struggling to solve a problem based on Python. I was solving basic-level problems from a website. I’ve got a question today that says to find the intersection of two lists using Python codes. I didn’t get the facts here. Could you please elaborate on the things clearly? Thanks!
asked Jul 7 adamSw 11.3k points
1 vote
1 answer 15 views
15 views
Problem: Hello good souls, var myarr1 = [1, 2, 3, 4, 5]; var myarr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; I have two simple arrays like above. I want to sort out the common values between those two arrays. So can you find common elements in two arrays javascript for me? Please keep the program as simple as possible that I can understand easily. Thanks for your effort and valuable time buddy.
asked Apr 6 Gavin 15.3k points