pythonzapier

Turn list of lists into key:value pairs in python


I have a system I am working with (Zapier!) which I am using to automate a workflow based on a google sheet which refreshes every hour. The Zap outputs raw row data in the following format:

list = [["header_1", "header_2", "header_3"], ["uuid_1", "first_timestamp_1", "second_timestamp_1"], ["uuid_2", "first_timestamp_2", "second_timestamp_2"], ["uuid_3", "first_timestamp_3", "second_timestamp_3"]]

What I am trying to do is transform this data into a json object that is a collection of key value pairs, with the first list element as the key and the remainder of the list elements as values. Ideally, my output would look like this:

[
  {
    "header_1":"uuid_1",
    "header_2":"first_timestamp_1",
    "header_3":"second_timestamp_1"
  }
  {
    "header_1":"uuid_2",
    "header_2":"first_timestamp_2",
    "header_3":"second_timestamp_2"
  }
  {
    "header_1":"uuid_3",
    "header_2":"first_timestamp_3",
    "header_3":"second_timestamp_3"
  }
]

This is the python I have thus far.

list = [["header_1", "header_2", "header_3"], ["uuid_1", "first_timestamp_1", "second_timestamp_1"], ["uuid_2", "first_timestamp_2", "second_timestamp_2"], ["uuid_3", "first_timestamp_3", "second_timestamp_3"]]

row_count = len(list)

if row_count == 1:
    print(null)
else:
    header = list[0]
    output = []
    for element in list:
        output.append(dict())
        for i in element:
            j = 0
            while j < len(header):
                key = header[j]
                value = i
                output = """+key+"":""+value+"""
                j = j+1

At this point, it looks like my code is erroring out when I try to append the new key:value pair to the ouput, but I'm not sure if this is even the right approach to take. The error message is:

AttributeError: 'str' object has no attribute 'append'

Any advice or help would be appreciated!


Solution

  • In your while loop, you are overriding the variable output - you originally made it a list, and now it is a string. When you go to the second iteration of your for loop, output is now a string that does not have the append function.

    I am guessing you are trying to create a new dictionary element where you save the string in the variable output. The correct syntax for doing so (assuming there is a pre-existing dictionary tempdic) is:

    tempdic[key] = value
    

    Overall, you code should look something like this:

    l = [["header_1", "header_2", "header_3"], ["uuid_1", "first_timestamp_1", "second_timestamp_1"], ["uuid_2", "first_timestamp_2", "second_timestamp_2"], ["uuid_3", "first_timestamp_3", "second_timestamp_3"]]
    
    row_count = len(list)
    
    if row_count == 1:
        print(null)
    else:
        header = l[0]
        output = []
        for element in l[1:]:
            tempdict={}
            for i in element:
                j = 0
                while j < len(header):
                    key = header[j]
                    value = i
                    tempdict[key]=value
                    j = j+1
            output.append(tempdict)
    

    As a side not, notice I changed your variable name list to l, list is a reserved object name in python and this could be confusing.