0

quick (trivial) question: I cannot find a way to store the output of a series of operation on an observable in an external variable. For instance something like that:

mylist = []
Observable.from_([1, 2, 3]).to_list().store(mylist)

Not sure this is very "reactive", but should be trivial.

Thanks in advance

C

Charlie
  • 1,750
  • 15
  • 20
  • What have you actually tried, and what went wrong with it/them? – Scott Hunter Aug 23 '17 at 14:06
  • For example: mylist = [] Observable.from_([1, 2, 3]).to_list().subscribe(lambda l: mylist=l). But it is not a valid python code. – Charlie Aug 23 '17 at 14:11
  • I could write my own Observer to store the data (the list) in some variable, but I am wondering if such functionality exists already – Charlie Aug 23 '17 at 14:16

2 Answers2

0

Here is a solution i found:

mylist = []

def store(value):
    mylist.append(value)

Observable.from_([1, 2, 3]).do_action(store).subscribe()

print(mylist)

Comments?

Thanks

Charlie
  • 1,750
  • 15
  • 20
0

Turn your observable into a blocking one via .to_blocking(). Now you can iterate over it.

mylist=list(Observable.from_([1, 2, 3]).to_blocking())                                                                   

print(mylist) 
Günther Jena
  • 3,706
  • 3
  • 34
  • 49