Combining Lists

I have two lists such that each list contains no repeated elements. However, the lists do share some elements, and I want to combine the lists such that no elements are repeated. For example, if list_1 is [ 1 , 2 , 4 ] [1,2,4] and list_2 is [ 1 , 3 , 5 ] [1,3,5] , then the combined list should be [ 1 , 2 , 4 , 3 , 5 ] , [1,2,4,3,5], not [ 1 , 2 , 4 , 1 , 3 , 5 ] . [1,2,4,1,3,5].

To accomplish this, what should go in place of "FILL ME IN" in the code below?

1
2
3
4
for element in list_2:
    if "FILL ME IN":
        list_1.append(element)
print list_1

element in list_1 element in list_2 element not in list_2 element not in list_1

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Vijay Kumar
Jan 20, 2016

This is a problem for noobs. Since the element is from list 2 we just wanna check whether it is in list 1 or not.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...