Alternative to Double Iteration

0 votes

I am trying to check each value in a List against every other value in the same List. If I identify something that meets some requirement, I add it to another List to be removed after this procedure is finished.

I have the following Pseudo-code:

for value1 in my_list:
    for value2 in my_list:
        if meets_requirements(value1, value2):
            to_be_removed.append(value2)

But the naming conventions for the variables are difficult to assign or understand. So is there any alternative way to do the same? Can anyone help me with a simpler method?

Apr 5 in Python by ana1504.k
• 7,430 points
17 views

1 answer to this question.

0 votes
You don't have to find the removed values first. Just create the list you need in one shot:

my_list = [y for y in my_list
             if not any(meets_requirement(x,y) for x in my_list)]
answered Apr 5 by SDeb
• 12,280 points

Related Questions In Python

+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
1,096 views
+2 votes
2 answers

How to use BeatifulSoup for webscraping?

your programme is fine until you start ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
23 views
+1 vote
2 answers

How to resize the image canvas to maintain a square aspect ratio

down voteacceptedUsing OpenCVYou can use resize() in OpenCV to ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
149 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,700 points
1,019 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 127 views
0 votes
1 answer

How to convert string into epoch time?

you are passing the parsed datetime object to ...READ MORE

answered Sep 21, 2018 in Python by SDeb
• 12,280 points
73 views
0 votes
1 answer

How to check the version of Python?

you can check the version of python ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 12,280 points
35 views