Unite or serach from two lists

Hello, I don’t know how I can figure out with my problem.
I have two list one where I should find IP address by MAC address,

[
    {
        "asset": {
            "mac": "00:50:56:9f:ae:a6",
            "ip": "192.168.16.102"
        }
    },
    {
        "asset": {
            "mac": "00:50:56:9f:3f:79",
            "ip": "192.168.16.103"
        }
    }
]

and second list

[
    {
        "ip_address": {
            "id": 3,
            "address": "192.168.16.102",
            "broadcast": "192.168.16.255",
            "created_at": "2020-07-03T14:35:51.000Z",
            "gateway": "192.168.16.5",
            "hypervisor_id": 1,
            "ip_range_id": 1,
            "ipv4": true,
            "network_address": "192.168.16.0",
            "prefix": 24,
            "pxe": true,
            "updated_at": "2020-07-03T15:09:20.000Z",
            "user_id": null
        }
    },
    {
        "ip_address": {
            "id": 4,
            "address": "192.168.16.103",
            "broadcast": "192.168.16.255",
            "created_at": "2020-07-03T14:35:51.000Z",
            "gateway": "192.168.16.5",
            "hypervisor_id": null,
            "ip_range_id": 1,
            "ipv4": true,
            "network_address": "192.168.16.0",
            "prefix": 24,
            "pxe": true,
            "updated_at": "2020-07-03T14:35:51.000Z",
            "user_id": null
        }
    },
]

where I should find ID by previously finded IP, I’m trying to use for_each to search IP

IP = [for x in data.assets.list : x.ip if x.mac == var.config[count.index]["mac"]][0]

and then second for_each inside the resource to find ID of IP address

resource "xxx" "yyy" {
  count                     = length(var.config)
  label                     = var.config[count.index]["label"]
  mac                       = var.config[count.index]["mac"]
  pxe_ip_address_id         = [for x in data.ipaddress.list.ip_address : x.id if x.address == <<< IP >>>][0]
}

But I can’t unite this two for_each to work with count.index
How I can do this?

locals {
  assets = [
    {
      asset : {
        "mac" : "00:50:56:9f:ae:a6",
        "ip" : "192.168.16.102"
      }
    },
    {
      asset : {
        "mac" : "00:50:56:9f:3f:79",
        "ip" : "192.168.16.103"
      }
    }
  ]
  flattened_assets = zipmap(flatten([
    for asset in local.assets : [
      for k, v in asset : [
        v["ip"]
      ]
    ]
    ]), flatten([
    for asset in local.assets : [
      for k, v in asset : [
        v["mac"]
      ]
    ]
  ]))
  addresses = [
    {
      ip_address : {
        "id" : 3,
        "address" : "192.168.16.102",
        "broadcast" : "192.168.16.255",
        "created_at" : "2020-07-03T14:35:51.000Z",
        "gateway" : "192.168.16.5",
        "hypervisor_id" : 1,
        "ip_range_id" : 1,
        "ipv4" : true,
        "network_address" : "192.168.16.0",
        "prefix" : 24,
        "pxe" : true,
        "updated_at" : "2020-07-03T15:09:20.000Z",
        "user_id" : null
      }
    },
    {
      ip_address : {
        "id" : 4,
        "address" : "192.168.16.103",
        "broadcast" : "192.168.16.255",
        "created_at" : "2020-07-03T14:35:51.000Z",
        "gateway" : "192.168.16.5",
        "hypervisor_id" : null,
        "ip_range_id" : 1,
        "ipv4" : true,
        "network_address" : "192.168.16.0",
        "prefix" : 24,
        "pxe" : true,
        "updated_at" : "2020-07-03T14:35:51.000Z",
        "user_id" : null
      }
    }
  ]
  flattened_addresses = flatten([
    for address in local.addresses : [
      for k, v in address : {
        address = v
      }
    ]
  ])
}

resource "null_resource" "get_mac_by_ip" {
  for_each = { for v in local.flattened_addresses : v["address"]["id"] => v["address"] }
  
  provisioner "local-exec" {
    command = "echo ${local.flattened_assets[each.value["address"]]}"
  }
}
null_resource.get_mac_by_ip["4"]: Provisioning with 'local-exec'...
null_resource.get_mac_by_ip["4"] (local-exec): Executing: ["/bin/sh" "-c" "echo 00:50:56:9f:3f:79"]
null_resource.get_mac_by_ip["3"]: Provisioning with 'local-exec'...
null_resource.get_mac_by_ip["3"] (local-exec): Executing: ["/bin/sh" "-c" "echo 00:50:56:9f:ae:a6"]
null_resource.get_mac_by_ip["4"] (local-exec): 00:50:56:9f:3f:79