-1

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"]
adobean
  • 314
  • 2
  • 12

6 Answers6

2

You can use set and do like this:

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

result = list(set(a + b + c))

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

ParthS007
  • 2,581
  • 1
  • 22
  • 37
1
a = ["apple", "banana", "orange"]
b = ["apple", "banana"]
c = ["cherry", "grappe"]

lst = list(set(a + b + c))
print(lst)
['banana', 'apple', 'cherry', 'grappe', 'orange']

Check out the docs for set to understand what it is doing.

felipe
  • 7,324
  • 2
  • 28
  • 37
1

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)
Wasif
  • 14,755
  • 3
  • 14
  • 34
1

If you want to keep the order of first appearance, as your sample output suggests, one way is to create a dict with your data as keys, taking advantage of the fact that dicts are ordered since Python 3.7:

from itertools import chain

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

d = {key: None for key in chain(a, b, c)}
result = list(d.keys())

print(result)
# ['apple', 'banana', 'orange', 'cherry', 'grappe']
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
1
a = ["apple", "banana", "orange"]
b = ["apple", "banana"]
c = ["cherry", "grappe"]
result = a + b + c  # marge all lsit
result = list(dict().fromkeys(result))  # remove duplicate
print(result)

Reference: https://www.w3schools.com/python/python_howto_remove_duplicates.asp

0

You can use set to get rid of duplicates:

list(set(a+b+c))
Szymon
  • 91
  • 6