Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
common elements of a list
#1
given 2 or more lists, i'd like to get a list of the common elements.  if the order gets re-arranged that is ok.

a=['apple','banana','cherry','lemon','orange']
b=['car','lemon',80,443,'python','banana','yellow']
c=commonlist(a,b)
print(c)
Output:
['banana','lemon']
is there an easy way to do that?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Convert lists to sets and check for intersection

a=['apple','banana','cherry','lemon','orange']
b=['car','lemon',80,443,'python','banana','yellow']
c=list(set(a) & set(b)) # of course converting back to list is optional
print c
Output:
['lemon', 'banana']
Reply
#3
set might be a better way to store certain "lists" where order does not matter.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
One with list comprehension.
>>> a = ['apple','banana','cherry','lemon','orange']
>>> b = ['car','lemon',80,443,'python','banana','yellow']
>>> [i for i in a if i in b]
['banana', 'lemon']
not change to difference.
>>> [i for i in a if i not in b]
['apple', 'cherry', 'orange']
# sets
>>> list(set(a) - set(b))
['cherry', 'apple', 'orange']
Reply
#5
List comprehension is more universal, as sets work only with hashable items. Sometimes one could be bitten with a fact that immutability doesnt guarantee hashability (usually with tuples - while (1, [2]) is tuple and therefore immutable, it is not hashable).
Reply
#6
(Mar-22-2017, 09:44 AM)zivoni Wrote: List comprehension is more universal, as sets work only with hashable items. Sometimes one could be bitten with a fact that immutability doesnt guarantee hashability (usually with tuples - while (1, [2]) is tuple and therefore immutable, it is not hashable).

Good point regarding hashability of elements of the set
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 4 2,581 Apr-11-2026, 05:22 AM
Last Post: logansummers
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 2,815 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 8,291 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 5,301 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 10,378 May-17-2022, 11:38 AM
Last Post: Larz60+
  a function common to methods of a class Skaperen 7 5,137 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Why am I getting list elements < 0 ? Mark17 8 5,788 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 3,627 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 4,428 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 3,319 Jul-11-2021, 09:58 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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