Python Math – TypeError: ‘NoneType’ object is not subscriptable

lista = list.sort(lista) This should be lista.sort() The .sort() method is in-place, and returns None. If you want something not in-place, which returns a value, you could use sorted_list = sorted(lista) Aside #1: please don’t call your lists list. That clobbers the builtin list type. Aside #2: I’m not sure what this line is meant … Read more

Sort a part of a list in place

I’d write it this way: a[i:j] = sorted(a[i:j]) It is not in-place sort either, but fast enough for relatively small segments. Please note, that Python copies only object references, so the speed penalty won’t be that huge compared to a real in-place sort as one would expect.

In-place edits with sed on OS X

You can use the -i flag correctly by providing it with a suffix to add to the backed-up file. Extending your example: sed -i.bu ‘s/oldword/newword/’ file1.txt Will give you two files: one with the name file1.txt that contains the substitution, and one with the name file1.txt.bu that has the original content. Mildly dangerous If you … Read more

Pandas: peculiar performance drop for inplace rename after dropna

This is a copy of the explanation on github. There is no guarantee that an inplace operation is actually faster. Often they are actually the same operation that works on a copy, but the top-level reference is reassigned. The reason for the difference in performance in this case is as follows. The (df1-df2).dropna() call creates … Read more

What is the difference between `sorted(list)` vs `list.sort()`?

sorted() returns a new sorted list, leaving the original list unaffected. list.sort() sorts the list in-place, mutating the list indices, and returns None (like all in-place operations). sorted() works on any iterable, not just lists. Strings, tuples, dictionaries (you’ll get the keys), generators, etc., returning a list containing all elements, sorted. Use list.sort() when you … Read more