First

See also

  • Official ReactiveX documentation: First
Observable.first(predicate=None)

Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.

Example: res = res = source.first() res = res = source.first(lambda x: x > 3)

Keyword arguments: predicate – {Function} [Optional] A predicate function to evaluate for

elements in the source sequence.

Returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate if provided, else the first item in the sequence.

../../_images/first.png ../../_images/B.first.p.png
Observable.first_or_default(predicate=None, default_value=None)

Returns the first element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.

Example: res = source.first_or_default() res = source.first_or_default(lambda x: x > 3) res = source.first_or_default(lambda x: x > 3, 0) res = source.first_or_default(null, 0)

Keyword arguments: predicate – {Function} [optional] A predicate function to evaluate for

elements in the source sequence.
default_value – {Any} [Optional] The default value if no such element
exists. If not specified, defaults to None.

Returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.

../../_images/firstOrDefault.png ../../_images/B.firstOrDefault.p.png
Observable.single(predicate=None)

Returns the only element of an observable sequence that satisfies the condition in the optional predicate, and reports an exception if there is not exactly one element in the observable sequence.

Example: res = source.single() res = source.single(lambda x: x == 42)

Keyword arguments: predicate – {Function} [Optional] A predicate function to evaluate for

elements in the source sequence.

Returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.

../../_images/single.png ../../_images/B.single.p.png
Observable.single_or_default(predicate, default_value)

Returns the only element of an observable sequence that matches the predicate, or a default value if no such element exists this method reports an exception if there is more than one element in the observable sequence.

Example: res = source.single_or_default() res = source.single_or_default(lambda x: x == 42) res = source.single_or_default(lambda x: x == 42, 0) res = source.single_or_default(None, 0)

Keyword arguments: predicate – {Function} A predicate function to evaluate for elements in

the source sequence.
default_value – [Optional] The default value if the index is outside
the bounds of the source sequence.

Returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.

../../_images/singleOrDefault.png ../../_images/B.singleOrDefault.p.png