Python Forum
Loop through elements of list and include as value in the dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through elements of list and include as value in the dictionary
#1
Hi,

I have a nested dictionary output from aws service from which I need to extract the cluster name and it's corresponding ID. I am storing cluster name as key in a dictionary. I want to include ID as value to the key which is cluster_name in this case.

cluster_name = {}
for cluster in cluster['ClusterInfoList']:
        cluster_name[cluster['ClusterName']] =[] 
        break

kb = []
for broker in kfk_broker['NodeInfoList']:
    kb = broker['BrokerNodeInfo']['BrokerId']
Output:
cluster_name output {'cluster1': [], 'cluster2':[]} kb output 1 2 3
I would like to assign the value of kb as value to the dictionary for clustername
Output:
Desired output {'cluster1': [1,2,3], 'cluster2':[1,2,3]}
Thanks in advance for the help
Reply
#2
how do you link cluster (the key) and brokerId? if possible, show sample input
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi @buran,

Thanks for your reply. All my cluster will have standard set of broker ID, meaning I need include same broker ID's to both cluster. Number of broker ID stored as a value for both key will be the same. I get cluster name and arn using boto3 aws function for kafka using list_clusters(). Then I use cluster arn as input to list_nodes() to get the broker ID's. So number of broker ID value associated with cluster names are the same.

Hope this helps
Reply
#4
Still not sure I get it right, but I would get the brokerID list first and then, assign that to each cluster name (as key)
I would do this in list/dict comprehension

brokers = [broker['BrokerNodeInfo']['BrokerId'] for broker in kfk_broker['NodeInfoList']]
clusters = {cl['ClusterName']:brokers[::] for cl in cluster['ClusterInfoList']}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,041 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,269 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,360 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,316 Sep-24-2020, 02:26 PM
Last Post: buran
  How can I print the number of unique elements in a list? AnOddGirl 5 3,291 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,930 Mar-02-2020, 10:17 AM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,046 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  Extracting elements in a list to form a message using for loop Tony04 2 2,369 Oct-25-2019, 05:55 PM
Last Post: ichabod801
  Sum of elements on the list recursive sev 3 2,564 Jun-20-2019, 02:14 PM
Last Post: sev
  Finding average in dictionary using for loop Rae 4 14,667 May-26-2019, 12:06 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020