Rodney
Rodney

Reputation: 41

Creating a list of dictionaries from another list of dictionaries

Sorry for the long post. I have been trying to figure this out for days. But, I'm stumped. Python is not my native language.

I am pulling a list of dictionaries containing information about 1,400 hosts from an API.

I convert the json data to a python list of dictionaries. I create a second list to be used to populate a new list of dictionaries, one with a subset of the information from the list pulled from the API.

I create a list of the keys I need the info from. Next, I create two for loops. The first iterates through the original list of dictionaries and the second iterates through the list of keys that I want to put into the new list of dictionaries.

If I add print statements in the two loops, I can confirm that I am iterating through the correct information that I am looking for and that this information is being added to the new list of dictionaries.

Both lists of dictionaries, the list of keys, and a new dictionary ( to be used in the loop ) are all defined as global in scope.

However, later in the script when I go to reference any specific element of the final list of dictionaries, all 1,400 dictionaries contain the same values from the last entry of the original list of dictionaries.

host_info is a list of dictionaries pulled from an API

host_fields is a list of keys that I want to parse from host_info

# New list of dictionaries. We will populate the keys in these
# from the host_fields list above.
export_list_of_dictionaries = []

# New dictionary for use in populating in export_list_of_dictionaries
new_host = {}

# Loop through the host_info list of dictionaries to pull
# the specific host_fields
for index in  range(len(host_info)):
    for field in host_fields:
        # Add the field as a key to the new_host dictionary
        new_host[field] = host_info[index][field]

    # **** The line above is cycling through the fields of host_fields correctly ****

# print(index) **** the index is cycling through host_info correctly ****
# Add the new_host dictionary to the new export_list_of_dictionaries
export_list_of_dictionaries.append(new_host)

# **** The print statement below shows that each of the elements has the correct ip
#print(export_list_of_dictionaries[index]['ip'])
# print(len(export_list_of_dictionaries)) **** This printed the correct number of elements ****

The keys from the original list of dicts print correctly. Each IP from host_info is different.

# Print the IP for the first element in each list of dictionary
print("IP from the first element of the original list of dictionaries")
print(host_info[0]['ip'])
print(host_info[1]['ip'])
print(host_info[-1]['ip'])

Here's where the trouble becomes apparent: However, the keys from the final list of dicts all have the same IP, which is incorrect.

print("IP from the first element of the final list of dictionaries")
print(export_list_of_dictionaries[0]['ip'])
print(export_list_of_dictionaries[1]['ip'])
print(export_list_of_dictionaries[-1]['ip'])

Simple answers only please, I'm new to Python.

Upvotes: 2

Views: 1189

Answers (1)

viniciusdgc
viniciusdgc

Reputation: 91

It seems like all entries in your list are references to the new_host object, which you're modifying on every loop. Try something like:

for index in  range(len(host_info)):
    # Add a new blank dict to the list
    export_list_of_dictionaries.append({})
    for field in host_fields:
        # Add the field as a key to the new element of the list
        export_list_of_dictionaries[index][field] = host_info[index][field]

Upvotes: 1

Related Questions