Let's create some Dask Bag objects using Python iterable items:
# Import dask bag
import dask.bag as db
# Create a bag of list items
items_bag = db.from_sequence([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], npartitions=3)
# Take initial two items
items_bag.take(2)
This results in the following output:
(1, 2)
In the preceding code, we created a bag of list items using the from_sequence() method. The from_Sequence() method takes a list and places it into npartitions (a number of partitions). Let's filter odd numbers from the list:
# Filter the bag of list items
items_square=items_bag.filter(lambda x: x if x % 2 != 0 else None)
# Compute the results
items_square.compute()
This results in the following output:
[1, 3, 5, 7, 9]
In the preceding code, we filtered the odd numbers from the bag of lists using the filter() method. Now, let's square each item of the bag using the map function:
# Square the bag of list items
items_square=items_b.map(lambda...