0

Just started learning list comprehension in Python and confusing a lot.

My code is:

    no_duplicates_list = []    
    for url in sorted_temp_list:
        found = False
        for index, url_for_comparing in enumerate(sorted_temp_list):
            if url in url_for_comparing and url != url_for_comparing:
                sorted_temp_list[index] = url
                found = True
        if found is True:
            no_duplicates_list.append(url)

    return no_duplicates_list

Already spent huge time with it but can't understand how to add boolean variable inside and variable assignment.

More details where I use it: if a url is parent, all other urls with the the first url as the prefix should be removed. Here is an example

  1. aaaccc.com/a
  2. aaaccc.com/a/b
  3. aaaccc.com/a/b/c
  4. aaaccc.com/a/c
  5. aaaccc.com/a/d
  6. aaaccc.com/a/e
  7. aaaccc.com/b/bc
  8. aaaccc.com/b/bc/a
  9. aaaccc.com/b/bc/b

In the above list since url 1 is in the list, rows 2 through 6 will inherit that reputation. Similarly, due to row 7, rows 8 and 9 also not necessary as they will inherit reputation from 7.

  • 2
    List comprehension is not for cramming all and sundry (e.g assignment) in between a pair of square braces. You generally would not execute statements in a list comprehension. – Moses Koledoye Nov 08 '17 at 10:21
  • To paraphrase Moses (in a somewhat more verbose way): all loops are not meant to be rewritten as list expressions, and one the best criterion is whether writing it as a list expression is harder than keeping it as a plain for loop. – bruno desthuilliers Nov 08 '17 at 10:23
  • so should I combine lambda expressions with it? I have just got a notice from others that for loops is not good idea. So Im looking for other solutions. Where do you think I should look for ideas? – Ivan Podorozhnyi Nov 08 '17 at 10:24
  • Actually, if you add a `break` in your `for` loop, it could well be more efficient than a list comp. anyway. After `found = True` add a `break` and use `if found:` instead of `if found is True` and it's not bad code from what I can see. – roganjosh Nov 08 '17 at 10:24
  • Removing duplicates from lists can be done as `list(set(mylist))`, see [this question](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) for details. – bgse Nov 08 '17 at 10:26
  • actually my task is a little bit harder than removing duplicates from list :) – Ivan Podorozhnyi Nov 08 '17 at 10:27
  • if a url is parent, all other urls with the the first url as the prefix should be removed. Here is an example 1. aaaccc.com/a 2. aaaccc.com/a/b 3. aaaccc.com/a/b/c 4. aaaccc.com/a/c 5. aaaccc.com/a/d 6. aaaccc.com/a/e 7. aaaccc.com/b/bc 8. aaaccc.com/b/bc/a 9. aaaccc.com/b/bc/b In the above list since url 1 is in the list, rows 2 through 6 will inherit that reputation. Similarly, due to row 7, rows 8 and 9 do not need to be deployed as they will inherit reputation from 7. Sorry for formatting :( – Ivan Podorozhnyi Nov 08 '17 at 10:29
  • updated the problem details for better understanding where I use it – Ivan Podorozhnyi Nov 08 '17 at 10:35

0 Answers0