I need to parse a the following list format into a dict.
This represents 2 people, with 3 pieces of data for each person, firstname, lastname, & email.
data = [3, 'firstname', 'lastname', 'email', 2, 'jack', 'black', 'jb@example.com', 'jane', 'green', 'jg@examlpe.com']
data[0] is how many people fields are being sent. (3)
The next 3 items are the property names.
Before the actual person data starts, the 2 represents how many people are to follow.
I’m new to Python and I’m wondering if there’s a slick way to do this. I feel like there’s got to be a better way.
(untested code, just showing the only way i can think to approach it)
def formatPeople(self, data):
param_count = data[0]
keys = []
pos = 1
while pos <= param_count:
keys.append(data[pos])
pos += 1;
people_count = int(data[pos])
pos += 1
people = []
i = 0
while i < people_count:
people_data = {}
for x in keys:
people_data[keys[x]] = data[pos]
pos += 1
people.append(people_data)
return people
That’s a good list to use iterators: