# How do you loop a list using index in Terraform?

**URL:** <https://discuss.hashicorp.com/t/how-do-you-loop-a-list-using-index-in-terraform/8587>\
**Category:** Terraform\
**Tags:** learn\
**Created:** [May 10, 2020, 9:39am UTC](https://discuss.hashicorp.com/t/how-do-you-loop-a-list-using-index-in-terraform/8587 "2020-05-10T09:39:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsuhendro](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rsuhendro/32/6750_2.png) [@rsuhendro](https://discuss.hashicorp.com/u/rsuhendro)\
**Post date:** [May 10, 2020, 9:39am UTC](https://discuss.hashicorp.com/t/how-do-you-loop-a-list-using-index-in-terraform/8587/1 "2020-05-10T09:39:43Z")

</div>

A newbie question. How do you do the following python code in Terraform?

```
count = 0
mylist = []
while count < 5:
    mylist.append("anystring"+ str(count))
    count += 1 

```

Thanks for any hint  
Hendro

---

<div class="post-metadata">

**Author:** ![rsuhendro](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rsuhendro/32/6750_2.png) [@rsuhendro](https://discuss.hashicorp.com/u/rsuhendro)\
**Post date:** [May 10, 2020, 11:29am UTC](https://discuss.hashicorp.com/t/how-do-you-loop-a-list-using-index-in-terraform/8587/2 "2020-05-10T11:29:43Z")

</div>

Well, the real case that I need to do is to build the connection string to mongoDB replicaset service. The replication\_factor is 3, 5 or 7.

The terraform code that sofar I built:

```
 locals {
  uri_list = ["@${alicloud_mongodb_instance.tyk_mongodb-test.id}1.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}2.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}3.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}4.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}5.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}6.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}" 
  ]

  uri_list_out = [
      for uriname in local.uri_list:
           lower(uriname) 
           if substr(uriname,length("${alicloud_mongodb_instance.tyk_mongodb-test.id}")+1,1) < (var.mongos_config["${var.environment}"]["replication_factor"])
  ] 
.
.  
.

```

What I expect from

```
output "uri_list_out" {
  value = local.uri_list_out
}

```

is the firts two elements of uri\_list but instead I got only [1,2] for replication\_factor = 3. Seems **if** instruction in **for** also modify the output ???

Appreciate for any hints…

Hendro

---

<div class="post-metadata">

**Author:** ![rsuhendro](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rsuhendro/32/6750_2.png) [@rsuhendro](https://discuss.hashicorp.com/u/rsuhendro)\
**Post date:** [May 10, 2020, 6:14pm UTC](https://discuss.hashicorp.com/t/how-do-you-loop-a-list-using-index-in-terraform/8587/3 "2020-05-10T18:14:35Z")

</div>

I just realize that for terraform instead of using loop, my problem can be solved using simple lookup element() 😃
