Python Forum
finding similar objects in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding similar objects in a list
#1
I am new to python and programming in general.I wanted to write a code which prints the similar elements from 2 lists .For example if a=[1,2,3,4,5] and b=[0,2,4,6] , I would want a c=[2,4]. Here is what I did:

def simi(a,b):
    a,b,c=[],[],[]
    for i in a:
        for j in b:
            if i==j:
                c.append(i)
            else:
                continue
    return (c)
[python]
Reply


Messages In This Thread
finding similar objects in a list - by DRPR - Jun-22-2018, 06:57 AM
RE: finding similar objects in a list - by buran - Jun-22-2018, 07:09 AM
RE: finding similar objects in a list - by DRPR - Jun-22-2018, 09:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error is finding mean of a list PythonBoy 4 923 Sep-11-2023, 02:38 PM
Last Post: PythonBoy
  Finding combinations of list of items (30 or so) LynnS 1 885 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,657 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,577 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
Question Finding string in list item jesse68 8 1,892 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  How to store the resulting Doc objects into a list named A xinyulon 1 1,906 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  Grouping and sum of a list of objects Otbredbaron 1 3,226 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Sum similar items tester_V 3 1,984 Jun-29-2021, 06:58 AM
Last Post: tester_V
  Passing List of Objects in Command Line Python usman 7 3,206 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,051 Aug-14-2020, 12:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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