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, 2019 in Python by ana1504.k
• 7,910 points
675 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, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

How to check the value is double in the existing data that when ever the values are double print thats value

Hey, @There, I guess regarding your query you ...READ MORE

answered Nov 3, 2020 in Python by Gitika
• 65,770 points
848 views
+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,720 points
4,903 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,720 points
1,066 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,720 points

edited Jul 11, 2023 by Khan Sarfaraz 1,645 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

How to convert string into epoch time?

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

answered Sep 22, 2018 in Python by SDeb
• 13,300 points
6,787 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
• 13,300 points
795 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP