How to compare elements in two separate lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avatar19
    New Member
    • Apr 2010
    • 43

    How to compare elements in two separate lists

    Hi, I would like to compare elements in two seperate Lists for example:
    Code:
    List1=[['Primi ', ' Movida ', ' 5'], ['Primi ', ' CCHQ ', ' 40'], ['Primi ', ' Chicago ', ' 45']]
    Code:
    List2=[['Chicago ', ' The Doors ', ' 20'], ['CCHQ ', ' The Doors ', ' 10']]
    What I understand is that the syntax should look like this:
    Code:
    List3
    for i in List2:
    	for j in List1:
                 if i[0]==j[1]
                     List3.append(j)

    What i specifically would like to do is that in List1. If List1[i][1] is found in List2[j][0] then i would like to pull out the List1 elements for which this is True.
    Please help I am very confused!
    Thanks
  • Nonemsludo
    New Member
    • Apr 2010
    • 15

    #2
    Code:
     d=[i[1] for i in List1 if i[1]==j[0] for j in List2]
    return d 
     f=[elem for elem in d if elem==elem in list1

    Comment

    • Avatar19
      New Member
      • Apr 2010
      • 43

      #3
      Thanks for the reply, i did manage to figure it out eventually but I am interested in this syntax, I've tried it an it doesn't seem to work??

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Something is not right about the code Nonemsludo posted. A return statement out of the blue?

        He was trying to show you a list comprehension. Here's my contribution:
        Code:
        List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
        Apparently you have some extra spaces. The strip() method gets rid of the spaces for the comparison to work.

        Comment

        • Avatar19
          New Member
          • Apr 2010
          • 43

          #5
          Hey bv thanks for the help, but just one thing I noticed is that with using "item" as the iteration variable in both lists seems to confuse the output, but otherwise perfect solution, Thanks!!

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            I am thinking the inner list comprehension is executed first then the outer. I like using item as the variable name for list iteration.

            Comment

            • Avatar19
              New Member
              • Apr 2010
              • 43

              #7
              Oh ok, I see it does work I just reversed the Lists and it gave the right result, is this how it was meant to be?

              original:
              Code:
              List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
              reversed:
              Code:
              List3=[item for item in List2 if item[0].strip() in[item[1].strip() for item in List1]]

              Thanks for the help bv
              Ava

              Comment

              Working...