Pydoc Pydoc

Takes a collection of key-value pairs and returns a collection of key-value pairs which has each key and value swapped.


In the following example, we create a pipeline with a PCollection of key-value pairs. Then, we apply KvSwap to swap the keys and values.

import apache_beam as beam

with beam.Pipeline() as pipeline:
  plants = (
      | 'Garden plants' >> beam.Create([
          ('🍓', 'Strawberry'),
          ('🥕', 'Carrot'),
          ('🍆', 'Eggplant'),
          ('🍅', 'Tomato'),
          ('🥔', 'Potato'),
      | 'Key-Value swap' >> beam.KvSwap()
      | beam.Map(print))

Output PCollection after KvSwap:

('Strawberry', '🍓')
('Carrot', '🥕')
('Eggplant', '🍆')
('Tomato', '🍅')
('Potato', '🥔')
Run code now Run code now
View source code View source code

Pydoc Pydoc