Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python – TypeError: object of type 'zip' has no len()

I have a GeoJSON file with many features in it and it is in the following format:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "fid": 109,
                "unique_property_number": 787387.0,
                "unique_building_number": 7627864.0,
                "property_area": 91.5,
                "building_area": 91.5,
                "mapping_block_number": "FH0801",
                "height": 5.8,
                "age": "MODERN",
                "use": "UTILITIES",
                "data_level": "FHAU",
                "data_link": null,
                "join_on": null
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -335013.4478176794,
                            7597020.21467809
                        ],
                        [
                            -335034.52685820695,
                            7597002.6574345445
                        ],
                        [
                            -335041.3940838162,
                            7597010.936560697
                        ],
                        [
                            -335020.31503652286,
                            7597028.493842001
                        ],
                        [
                            -335013.4478176794,
                            7597020.21467809
                        ]
                    ]
                ]
            }
        },       
etc

But I am trying to add a name field within the properties of each of the features by using this python script:

import json

with open('C:/inputfile.json', 'r') as f:
    data = json.load(f)

#A Python dictionary containing properties to be added to each GeoJSON Feature
properties_dict={
    "name": "Building"
    }
#Convert the dictionary to a list
properties_list=zip(properties_dict.keys(),properties_dict.values())

#Loop over GeoJSON features and add the new properties
for feat in data['features']:
    for i in range(len(properties_list)):
        feat ['properties'][properties_list[i][0]]=properties_list[i][1]

#Write result to a new file
with open('C:/outputfile.json', 'w') as f:
    json.dump(data, f)

However I am getting the below error (full traceback now included):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Traceback (most recent call last):
  File "c:\Users\Public\import os1.py", line 15, in <module>
    for i in range(len(properties_list)):
TypeError: object of type 'zip' has no len()

Can anyone help me pass this error message?

>Solution :

This is one of the reasons python discourages this pattern:

for i in range(len(properties_list)):

You don’t need to do this. zip objects are iterable, which means you can iterate directly over them:

properties_list=zip(properties_dict.keys(),properties_dict.values())

for feat in data['features']:
    for pair in properties_list:

Further, you don’t need zip at all. You are recreating a dict method that already exists: .items() which is also iterable and allows the much simpler:

properties_dict={"name": "Building"}

for feat in data['features']:
    for key, value in properties_dict.items():
        feat['properties'][key]=value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading