Skip to main content
added 14 characters in body
Source Link
Sp3000
  • 61.9k
  • 13
  • 115
  • 287

None arguments in Python 2 builtins

map (Python 2 only)

Mapping with None in place of a function assumes the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

However, as @KSab notes, if all elements are of the same type then there may be shorter alternatives, e.g. filter(str,L) if all elements are strings.

None arguments in Python 2 builtins

map

Mapping with None in place of a function assumes the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

However, as @KSab notes, if all elements are of the same type then there may be shorter alternatives, e.g. filter(str,L) if all elements are strings.

None arguments in Python builtins

map (Python 2 only)

Mapping with None in place of a function assumes the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

However, as @KSab notes, if all elements are of the same type then there may be shorter alternatives, e.g. filter(str,L) if all elements are strings.

edited body
Source Link
Sp3000
  • 61.9k
  • 13
  • 115
  • 287

None arguments in Python 2 builtins

map

Mapping with None in place of a function assumedassumes the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

However, as @KSab notes, if all elements are of the same type then there may be shorter alternatives, e.g. filter(str,L) if all elements are strings.

None arguments in Python 2 builtins

map

Mapping with None in place of a function assumed the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

None arguments in Python 2 builtins

map

Mapping with None in place of a function assumes the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]

However, as @KSab notes, if all elements are of the same type then there may be shorter alternatives, e.g. filter(str,L) if all elements are strings.

Source Link
Sp3000
  • 61.9k
  • 13
  • 115
  • 287

None arguments in Python 2 builtins

map

Mapping with None in place of a function assumed the identity function instead. This allows it to be used as an alternative to itertools.izip_longest for zipping lists to the length of the longest list:

>>> L = [[1, 2], [3, 4, 5, 6], [7]]
>>> map(None,*L)
[(1, 3, 7), (2, 4, None), (None, 5, None), (None, 6, None)]

For visualisation (with . representing None):

1 2                1 3 7
3 4 5 6      ->    2 4 .
7                  . 5 .
                   . 6 .

filter

filter with None also assumes the identity function, thus removing falsy elements.

>>> L = ["", 1, 0, [5], [], None, (), (4, 2)]
>>> filter(None, L)
[1, [5], (4, 2)]

This is a bit better than a list comprehension:

filter(None,L)
[x for x in L if x]