+ 9
How to find union of two lists?
I have two lists: A=[ "python" , "c" , "Java" , "perl" ] B=[ "php" , "JavaScript" , "python" , "SQL" , "Java" ] how can I get union of both lists?
11 Respuestas
+ 7
thanks Jan Markus and Mohit the coder (M.S.D) for this help.
+ 3
Jan Markus yeah I read it wrong check now I updated my code for numbers
+ 3
Also if you don’t want to turn the lists into sets, you can do:
inter = [s for s in A if s in B]
A_ = [s for s in A if s not in B]
B_ = [s for s in B if s not in A]
U = inter + A_ + B_
+ 3
Ok
newlist = list(set(list1) | set(list2))
i think this will work
+ 2
AISHWARYA KASTHALA
ya it will be fine. 😊
But I find it somehow useless to convert lists into sets before taking union as u have done.
print ("newlist :", list1 l list2) will simply do the job.
+ 2
can u answer this:
how to write a menu based program to insert,sort and reverse a list of items
+ 1
You can create a function
def union(list1,list2):
newlist = list1+ list2
return newlist
list1 = ['python','c','java','perl']
list2 = ['php','javascript','python','sql','java,]
print(union(list1,list2))
+ 1
AISHWARYA KASTHALA
but in your output 'java' and 'python' will repeat ... which is not allowed when we r taking union of two lists.
+ 1
AISHWARYA KASTHALA
I am also a beginner though .. not having enough knowledge of sorting, inserting stuffs.
I suggest u to post it in your wall .. some expert will surely answer it.
0
I like Python