listcomp

#python #programming #language #efficiency

  • It does what filter and map do.

Example of listcomp or list comprehension:


elems = [1 2 3 4]
aux = [elem for elem in elems]

listcomp create a new list, so it is necessary to allocate a whole list to manipulate a listcomp. There is a special case known as Walrus operator.

listcomp use more memory than genexp because last one does not allocate and deal with the element one by one.

Both sort and sorted are made to sort sequences. However, sort does is in-place while sorted produce another object.

Links to this page
#python #programming #language #efficiency