Skip to main content

About zip context method

You can use the zip context method to return an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables. For more information, see Python docs.

Args:

  • *args: Any number of iterables
  • default: A default value to return if *args is not iterable

Usage

{% set my_list_a = [1, 2] %}
{% set my_list_b = ['alice', 'bob'] %}
{% set my_zip = zip(my_list_a, my_list_b) | list %}
{% do log(my_zip) %} {# [(1, 'alice'), (2, 'bob')] #}
Report incorrect code
{% set my_list_a = 12 %}
{% set my_list_b = ['alice', 'bob'] %}
{% set my_zip = zip(my_list_a, my_list_b, default = []) | list %}
{% do log(my_zip) %} {# [] #}
Report incorrect code

zip_strict

You can use the zip_strict context to return an iterator of tuples, just like zip. The difference to the zip context method is that the zip_strict method will raise an exception on a TypeError, if one of the provided values is not a valid iterable.

Args:

  • value: The iterable to convert (e.g. a list)
{% set my_list_a = [1, 2] %}
{% set my_list_b = ['alice', 'bob'] %}
{% set my_zip = zip_strict(my_list_a, my_list_b) | list %}
{% do log(my_zip) %} {# [(1, 'alice'), (2, 'bob')] #}
Report incorrect code
{% set my_list_a = 12 %}
{% set my_list_b = ['alice', 'bob'] %}
{% set my_zip = zip_strict(my_list_a, my_list_b) %}

Compilation Error in ... (...)
'int' object is not iterable
Report incorrect code

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

0
Loading