Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:35:58+00:00 2026-05-27T23:35:58+00:00

I have two python lists as follow: ListA = [a1,a2,a1,a3,a2,a4,a5,a4] ListB = [b1,b2,b1,b3,b2,b4,b5,b4] What

  • 0

I have two python lists as follow:

ListA = [a1,a2,a1,a3,a2,a4,a5,a4]
ListB = [b1,b2,b1,b3,b2,b4,b5,b4]

What I want is to find the equal elements in the two lists and print them in a file. I have found the equal elements in lists and add them to two new lists as follow:

[a1,a2,a4]
[b1,b2,b4]

I want to compare some parameters with elements in ListA and if an element in ListA and the parameter is equal print the corresponding element in ListB. I do this as follow.

for i,j in enumerate(ListA):
    if j == paramname:
        filelines.append('%sTransferSourceName = "%s"\n'%(indent,ListB[i]))

My problem is that the element are not in the order for ListB. It is added like below:

b2,b4,b1

So the whole order get mixed up.
Note that the number of letters in each element in the lists may differ.

Here is the code I have done so far:

def ProcessLinks():

duplicates = [x for x in linkparamArray if linkparamArray.count(x) > 1]

linkstemp = list(set(duplicates))
for i in linkstemp:

    links.append(i)

def ProcessLinks2():

duplicates2 = [x for x in linkparameterArray if linkparameterArray.count(x) > 1]

linkstemp2 = list(set(duplicates2))
for j in linkstemp2:

    linkparameters.append(j)

And here is the comparing code:

paramname = a1

for i,j in zip(linkparameters,links):

                if i == paramname:
                    filelines.append('%s         TransferSourceName = "%s"\n(indent,j))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:35:59+00:00Added an answer on May 27, 2026 at 11:35 pm

    You can use collections.Counter to determine duplicate elements.

    >>> ListA = ['a1','a2','a1','a3','a2','a4','a5','a4']
    >>> ListB = ['b1','b2','b1','b3','b2','b4','b5','b4']
    >>> cB=collections.Counter(ListB)
    >>> cA=x=collections.Counter(ListA)
    >>> [i for i in cA if cA[i]>1]
    ['a1', 'a2', 'a4']
    >>> [i for i in cB if cB[i]>1]
    ['b4', 'b1', 'b2']
    

    As per the Comment of OP as the order is important than the following solution might work using (OrderedDict)[http://docs.python.org/library/collections.html#collections.OrderedDict]

    dB=collections.OrderedDict()
    dA=collections.OrderedDict()
    >>> for a in ListB:
        dB.setdefault(a,0)
        dB[b]+=1
    
    >>> for b in ListB:
        dB.setdefault(b,0)
        dB[b]+=1
    
    >>> [i for i in dA if dA[i]>1]
    ['a1', 'a2', 'a4']
    >>> [i for i in dB if dB[i]>1]
    ['b1', 'b2', 'b4']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to find the intersection of two lists in python. i have something
In Python, I have two lists that either have equal number of elements (e.g.
I have two lists in python and I want to know if they intersect
If I have two parallel lists and want to sort them by the order
I have two columns in a text file. I read them into Python into
I want to do this. I have two python lists, one larger than the
In Python, lists have two methods for adding elements to the end: append and
I have two Python lists of dictionaries, entries9 and entries10 . I want to
I have found two ways to extract matches in Python: 1. def extract_matches(regexp, text):
I have two python lists passlist1= [[1, Saturday], [6, Sunday], [12 ,Wednesday], [31, Monday],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.