0

so I have two python lists of ids with other info that I am looping through. I try and match up the ids and assign row values based on the id join.

the example below is just a generic test on what I am actually doing but once I understand how to solve what I need I will be able to apply it to my example.

for row in l1:
    for info in l2:
        if row[0]==info[0]:
            row[1]=info[1]

each list respectively has about 11,000 rows in it so looping through the l1 then looping through every row in the l2 list to match up the ids takes a very long time. I was wondering if there is to say, okay since l1 id number 3 got iterated through and had the process finish for that id, then when the next row in l1 is being iterated l2 will now no NOT to even search for id number 3 which was processed already in the row before it.

I usually run a process like this in SQL, where it would sort of like a self join...

if somebody downvotes can they at least tell me why!

3
  • 1
    i upvoted to make up for the guy just down voting every question he doesn't like. but i don't get your question, can you elaborate more? Commented Feb 8, 2018 at 17:12
  • yes, so when I iterate through l1, each time it is then iterating through the entire l2 until it matches up row[0]==info[0] Commented Feb 8, 2018 at 17:13
  • what I want to do, is once that row[0] from l1 is done I want to not include that id to be looped through in l2 because it is just wasting time looping through a row that is already completed Commented Feb 8, 2018 at 17:15

2 Answers 2

1

Just elaborating on the previous answer, if you add break at the end of the if statement, once the if statement executes, it will stop the inside for loop for info in l2

and continue with the outer for loop for row in l1

Sign up to request clarification or add additional context in comments.

Comments

0

if i'm not wrong , and got your question correctly, you can use loop flow control structures , like

break

and

continue

for example in your case, you can break the inner loop when the match occurs and therefore your code won't waste more time looping through all l2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.