How to convert a list of topic to a map in terraform pipeline without impacting existing topic created

Currently we are using following code to create topic

resource "kafka_topic" "topic" {
  count              = length(var.topics)
  name               = "${lookup(var.topics[count.index], "is_public") ? "public" :"private"}_${var.environment}_${var.data_domain}_${lookup(var.topics[count.index], "name")}_${lookup(var.topics[count.index], "version")}"
  partitions         = lookup(var.topics[count.index], "partition_count") == null ? 6 : "${lookup(var.topics[count.index], "partition_count")}"
  replication_factor = 3
  config = {
    "cleanup.policy" = lookup(var.topics[count.index], "is_cleanup_policy_compact") ? "compact" : "delete"
    "max.message.bytes" =  lookup(var.topics[count.index], "max_message_bytes") != -1 ? "${lookup(var.topics[count.index], "max_message_bytes")}" : 1000012
  }
}

and passing topic as a list like

locals {
  iddn_news_cms_kafka_topics = [
    {
      name                      = "topic1"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
    },
	{
      name                      = "topic2"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
    }}}

but when we are removing a topic in between that then it will destroy the topic that came down in the sequence and recreate it as we are passing it as a list which works on index of element.

I have tried this set of code also

 for_each   = {
    for index, topic in var.topics:
    topic.name => topic
  }
  name               = "${each.value["is_public"] == true ? "public" :"private"}_${var.environment}_${var.data_domain}_${each.value["name"]}_${each.value["version"]}"

but when tried terraform plan it is showing the changes which should not happen in this case as topic are already created

is there any other alternative to do the changes without impacting the existing created topic

Please check your post and notice that the forum software has mangled it.

Use ``` markers to format code as code.