Search code examples
pythonarrayselementrepeatdifference

take one of the same elements in different strings


I want to combine the three different series I have. I also need to provide singularity for each element. Output order is not important.Example 3 arrays:

a=["apple","banana","orange"]
b=["apple","banana"]
c=["cherry","grappe"]

result

result=["apple","banana","orange","cherry","grappe"]

Solution

  • You can use itertools.chain then convert to set to remove duplicates and then convert back to list (done a sort too):

    import itertools
    a=["apple","banana","orange"]
    b=["apple","banana"]
    c=["cherry","grappe"]
    d = sorted(list(set(list(itertools.chain(a,b,c)))))
    print(d)