• Register
1 vote
30 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

1 vote
2 answers 68 views
68 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, 2020 LizzyM 6.1k points
0 votes
1 answer 42 views
42 views
Problem: I have two ArrayList objects with three integers each. I want to find a way to return the common elements of the two lists Use Collection#retainAll(). listA.retainAll(listB); // listA now contains only the elements which are also contained in listB.
asked Nov 6, 2020 Mashhoodch 13k points
0 votes
1 answer 27 views
27 views asked Oct 23, 2020 Shaad Sh 800 points
1 vote
1 answer 22 views
22 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, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 29 tuhin1 47.2k points
1 vote
1 answer 48 views
48 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, 2020 adamSw 11.3k points
1 vote
1 answer 4 views
4 views
Problem: Please help me. How can I solve it? python check if two lists have same elements
asked Mar 30 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 14 views
14 views
Problem: but I'm having a hard time finding the answer. When I compare 2 lists, I want to know if they are "equal" in that they have the same contents, but in different order. Ex: x = ['a', 'b'] y = ['b', 'a'] I want x == y to evaluate to True.
asked Feb 20 Mashhoodch 13k points
0 votes
1 answer 6 views
6 views
Problem: How to I Solve this: python sort list of lists by first element HELP! I need help understanding this: python sort list of lists by first element Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points
0 votes
1 answer 15 views
15 views
Problem: I wonder whether there is a shortcut to make a simple list out of list of lists in Python. I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with reduce(), but I get an error. Code l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(lambda x, y: x.extend(y), l)
asked Feb 24 Mashhoodch 13k points