Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists union and intersecion
#1
Hi

Is it possible to take the union and the intersection of lists. If affirmative, which module contains these methods, and where to find it ?

Thank you for the advice

Arbiel
Reply
#2
what exactly union and intersect would do on lists? Usisally these are terms applied to sets. Provide sample input and desired output
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yes, those terms apply to mathematical entities, with the following meaning :
Quote:list1=['a', 'b']
list2=['c']

union (list1, list2) : ['a', 'b, 'c']
intersection (list1, list2) : []
>>> dia
['Dasia', 'Dialytika', 'Macron', 'Oxia', 'Perispomeni', 'Prosgegrammeni', 'Psili', 'Tonos', 'Varia', 'Vrachy', 'Ypogegrammeni']
>>> mots
['Greek', 'Small', 'Letter', 'Upsilon', 'with', 'Dialytika', 'and', 'Oxia']
>>>
Quote:intersection (dia,mots) : ['Dialytika', 'Oxia']

Arbiel
Reply
#4
List and sets are different types in python. Sets hold unique/non-repeating elements, while lists can have repeating elements and there comes the tricky part.

As I said union and intersection are terms for sets. There is list.extend() method and for "intersection" you need to use list comprehension or loop

dia = ['Dasia', 'Dialytika', 'Macron', 'Oxia', 'Perispomeni', 'Prosgegrammeni', 'Psili', 'Tonos', 'Varia', 'Vrachy', 'Ypogegrammeni']
mots = ['Greek', 'Small', 'Letter', 'Upsilon', 'with', 'Dialytika', 'and', 'Oxia']

# convert to sets
result = set(dia).union(set(mots))
print(result)
print(list(result))

result2 = set(dia).intersection(set(mots))
print(result2)
print(list(result2))

# if order is imporatnt
result = [element for element in dia if element in set(mots)]
print(result)

dia.extend(mots)
print(dia)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hi buran

Thank you for your help. This exactly what I am looking for.

However I do not understand
Quote:you need to use list comprehension or loop
when I see that
result2 = set(dia).intersection(set(mots))
leads to the intersection of the sets.

Arbiel
Reply
#6
This is one way to do it
# convert to sets
result = set(dia).union(set(mots))
print(result)
print(list(result))
 
result2 = set(dia).intersection(set(mots))
print(result2)
print(list(result2))
However, this will not preserve order and set have unique elements, so if there are duplicates in the list (same element more than one time in list) they will be lost


And this is working with lists - order and repeating elements are preserved
# if order is imporatnt
result = [element for element in dia if element in set(mots)] # this is list comprehension, but it can be replaced by regular loop
print(result)
 
dia.extend(mots)
print(dia)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheritance vs Union gserranowong 3 2,237 Jun-29-2021, 06:13 PM
Last Post: gserranowong
  ImportError: cannot import name 'Union' from '_ctypes' (unknown location) ciuffoly 15 10,411 Oct-09-2020, 06:58 AM
Last Post: ciuffoly
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,368 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,262 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Union of dictionaries (taking max value on collision) meee 5 3,746 Jan-17-2018, 09:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020