Python Forum
Output difference from 2 lists of different sizes with words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output difference from 2 lists of different sizes with words
#1
Hi everyone.

Let say I have the following 2 lists with words of different sizes for example,
list0=['guava','apple','banana','strawberry','orange']
list1=['orange','pineapple','apple']
difference=list(set(list0).symmetric_difference(list1))
print(difference)
Output:
['banana', 'guava', 'strawberry', 'pineapple']
Output above is the mix of words that are not present in both sets that I am only able to do at this stage.

However, I'd like to seek your help on how can I achieve the the following desired output please? Huh
Output:
Words appear in first list and not second list = guava, banana, strawberry Words appear in second list and not first list = pineapple
Thanks a lot and have a great weekend
Reply
#2
you should use builtin set types, see: https://docs.python.org/3/library/stdtyp...0types#set
gracenz likes this post
Reply
#3
You can use set operation to achieve desired result:

>>> list0=['guava','apple','banana','strawberry','orange']
>>> list1=['orange','pineapple','apple']
>>> set(list0) - set(list1)
{'strawberry', 'guava', 'banana'}
>>> set(list1) - set(list0)
{'pineapple'}
However, sets are unordered. If order is important then list comprehension could be used:

>>> [item for item in list0 if item not in list1]
['guava', 'banana', 'strawberry']
>>> [item for item in list1 if item not in list0]
['pineapple']
gracenz likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Sep-02-2022, 02:55 PM)Larz60+ Wrote: you should use builtin set types, see: https://docs.python.org/3/library/stdtyp...0types#set

Thanks for the info Smile
Reply
#5
(Sep-02-2022, 03:48 PM)perfringo Wrote: You can use set operation to achieve desired result:

>>> list0=['guava','apple','banana','strawberry','orange']
>>> list1=['orange','pineapple','apple']
>>> set(list0) - set(list1)
{'strawberry', 'guava', 'banana'}
>>> set(list1) - set(list0)
{'pineapple'}
However, sets are unordered. If order is important then list comprehension could be used:

>>> [item for item in list0 if item not in list1]
['guava', 'banana', 'strawberry']
>>> [item for item in list1 if item not in list0]
['pineapple']

Thanks for the solutions Smile
Reply
#6
I'd do it this way:
>>> set1 = set(['guava','apple','banana','strawberry','orange'])
>>> set2 = set(['orange','pineapple','apple'])
>>> set3 = set(['banana', 'guava', 'strawberry', 'pineapple'])
>>> set1.difference(set2)
{'strawberry', 'banana', 'guava'}
>>> set3.difference(set1)
{'pineapple'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a table with different sizes of columns in MS word pepe 8 1,583 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Printing effect sizes for variables in an anova eyavuz21 2 990 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,821 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Difference in list output OokaydO 6 3,331 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  converting data sizes like: 4k, 32k, 4m, 16m, 1g, etc Skaperen 1 2,191 Jul-16-2019, 01:37 AM
Last Post: Skaperen
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,291 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How to format text to modify itself for different console sizes. FWendeburg 0 1,603 Feb-18-2019, 06:43 PM
Last Post: FWendeburg
  Compare all words in input() to all words in file Trianne 1 2,776 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Convert file sizes: will this produce accurate results? RickyWilson 2 8,118 Dec-04-2017, 03:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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