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 9197081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:59:39+00:00 2026-06-17T21:59:39+00:00

I was wondering if you could help me to speed up my python script.

  • 0

I was wondering if you could help me to speed up my python script.

I have two lists:

a=['a','b','c','d','e','f','g','h','i','j']

b=['b','f','g','j']

I want to create a list that will contain elements of b, but will have a length of a, with elements not in b replaced by something else, let’s say '-999'. Also, instead of having the actual elements (a,b,c…) I want to substitute that with the element’s index from b. So it would look like that:

c=['-999',0,'-999','-999','-999', 1, 2,'-999','-999',3] 

My code for now is:

c=[]

counter=0

for each in a:
    if each in b:
        c.append(counter)
        counter+=1
    else:
        c.append('-999')

It works fine, however, in real life my list a is 600 000 elements long, and there are actually 7 b lists that I need to iterate them over, all between 3k and 250k elements as well.

Any ideas on how to speed this up?

  • 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-06-17T21:59:40+00:00Added an answer on June 17, 2026 at 9:59 pm

    If the elements in b are unique then you can try this:

    In [76]: a=['a','b','c','d','e','f','g','h','i','j']
    
    In [77]: b=['b','f','g','j']
    
    In [78]: dic={x:i for i,x in enumerate(b)}
    
    In [79]: dic
    Out[79]: {'b': 0, 'f': 1, 'g': 2, 'j': 3}
    
    In [81]: [dic.get(x,'-999') for x in a]
    Out[81]: ['-999', 0, '-999', '-999', '-999', 1, 2, '-999', '-999', 3]
    

    For repeated items you can use defaultdict(list):

    In [102]: a=['a','b','c','d','e','f','g','b','h','i','f','j']
    
    In [103]: b=['b','f','g','j','b','f']
    
    In [104]: dic=defaultdict(list)
    
    In [105]: for i,x in enumerate(b):
        dic[x].append(i)
       .....:     
    
    #now convert every value(i.e list) present in dic to an iterator.
    
    In [106]: dic={x:iter(y) for x,y in dic.items()}  
    
    In [107]: [next(dic[x]) if x in dic else '-999' for x in a]  #call next() if the key 
                                                                 #is present else use '-999'
    Out[107]: ['-999', 0, '-999', '-999', '-999', 1, 2, 4, '-999', '-999', 5, 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could help me with a Python problem I have.
I was wondering if you could help. I have a list of information; ClientNo
I was wondering if someone could help me. I have the following two tables:
I was wondering if someone could help me please. I have the following two
Wondering if somebody could help me figuring this out, I have a list; basically
Wondering if someone could help me. I have next to no knowledge with Ajax,
I was wondering if someone could help me with this. I have defined my
Hey there, was wondering if anyone could help a newbie on SQL and Python.
Python neophyte here. I was wondering if someone could help with the KeyError I
just wondering if anyone could help me. I have a file containing a line:

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.