2

I have a list of homes :

list1 = [home1, home2, home3, home4]

and I have another list of specific homes:

list2 = [ home6, home7, home8, home10]

Every home has a field date .I want to insert List2 into list1 depending on home.date

For example if home7.date < home1.date, so home7 will be inserted into list1 before home1

I tried to use two for loops but it seems to be very slow and there are many calculations done by the CPU

        for el in list1:
            for elt2 in list2:
                if el.date > elt2.date:
                    list1.insert((list1.index(el)),elt2)

PS : some dates are not set so they have a None value and I don't want to change the index of corresponded home in list1

Any ideas ?

martineau
  • 99,260
  • 22
  • 139
  • 249
pietà
  • 670
  • 1
  • 9
  • 31

3 Answers3

7

First thing first: modifying a list (or dict, set etc) while iterating over it is usually a very bad idea.

In your case, the simplest solution is probably to just merge the two lists first then sort the list using the key callback:

list1.extend(list2)
list1.sort(key=lambda x: x.date)
bruno desthuilliers
  • 68,994
  • 6
  • 72
  • 93
  • Thanks for your quick answer.. the problem is that some `x.date` are `None` and I don't want to change their indexes . I will update my post – pietà Oct 04 '17 at 12:32
  • @pietà this is a much more complex problem - and one your current code does not handle actually (you'd get `TypeError` exceptions trying to compare a `date` with `None`). – bruno desthuilliers Oct 05 '17 at 09:46
2

Edited:

list3 = (list1 + list2)
list3.sort(key = lambda x: x.date)
ShreyasG
  • 676
  • 4
  • 10
  • Will need to provide the key for sorting – AK47 Oct 04 '17 at 11:55
  • 2
    @ShreyasG, using `.sort()` will return `None` because it happens in-place. It should be `sorted(list1+list2)`. – James Oct 04 '17 at 11:56
  • Right, agree with both of you. Didn't check the syntax and forgot the key to sort. – ShreyasG Oct 04 '17 at 11:57
  • Actually, I would use `.sort()` with key, but after inserting the elements of `list2` to `list1`. After all, OP wants to modify the list, which might be referred to in other places. – tobias_k Oct 04 '17 at 11:58
  • 1
    `Won't this do?` should only be used as a rhetoric question when you're sure it works. Otherwise, it should be a comment ;) – Eric Duminil Oct 04 '17 at 11:58
  • Thanks for your quick answer.. the problem is that some x.date are None and I don't want to change their indexes . I updated my post – pietà Oct 04 '17 at 12:42
2

To return a new sorted list, you should use the sorted() built-in function:

sorted_list = sorted(list1 + list2, key=lambda x: x.date, reverse=True)
Edward Minnix
  • 2,561
  • 1
  • 10
  • 25
rachid el kedmiri
  • 1,856
  • 12
  • 32
  • 1
    `true` doesn't exist in Python. Also : you don't need `reverse` here. – Eric Duminil Oct 04 '17 at 11:55
  • 1
    @tobias_k: Because it's been copy-pasted from https://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-based-on-an-attribute-of-the-objects – Eric Duminil Oct 04 '17 at 11:56
  • indeed you don't need reverse, however, my intention is to show the possibility of reversing the order, @EricDuminil true this is not a simple paste it takes skill and deep understanding to understand the question, otherwise, i wouldn't modify x.date – rachid el kedmiri Oct 04 '17 at 11:58
  • Thanks for your quick answer.. the problem is that some x.date are None and I don't want to change their indexes . I updated my post – pietà Oct 04 '17 at 12:42