Generate a sequence in a playbook using the inventory host group length

I want to use inventory host groups length of hosts and inventory host group vars .
For ex.

[Test]
Host0
Host1
Host2
Host3
Host4

[Test: vars]
Port=1223

Based on length of test group
I want to generate a sequence like this
0,1,2,3,4

Can someone please suggest how can I achieve this Inside a playbook via jinja2?

Can I use range function in for loop

mysequence: "{{range(0, groups['Test']|length + 1)|list }}"

Hi Brian,
Thankyou for the response.
This generates a list of numbers.

But I want a string like this “0,1,2,3”

I kind of tried python join n didn’t work.

Can you please assist.

use the join filter

{{range(0, groups['Test']|length + 1)|list | join(',') }}

Thankyou much Kai