How to convert list of lists to dict key value pairs python
Question:
I have a list of lists like so:
splitted = [['OID:XXXXXXXXXXX1',
' street:THE ROAD',
'town:NEVERPOOL',
'postcode:M1 2DD',
'Name:SOMEHWERE',
'street:THE ROAD',
'town:NEVERLAND',
'postcode:M1 2DD'],
['OID:XXXXXXXXXXX2',
' Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD',
'Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD']]
I’d like to convert these to key values pairs like so:
{'OID': 'XXXXXXXXXXX1', ' street': 'THE ROAD', 'town': 'NEVERPOOL', 'postcode': 'M1 2DD', 'Name': 'SOMEWHERE', 'street': 'THE ROAD', 'town': 'NEVERPOOL', 'postcode': 'M1 2DD'}, {'MPXN': 'XXXXXXXXXXX2', ' Name': '30', 'street': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD', 'primaryName': '30', 'street1': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD'}
I am unable to find a way online to convert a list of lists into key-value pairs as a dict to then be consumed by pandas. The purpose of this is to convert the dicts into a pandas DataFrame so I can then consume it and work with it in a tabular format
The code I have used thus far is here:
output = []
for list in splitted:
for key_value in list:
key, value = key_value.split(':', 1)
if not output or key in output[0]:
output.append({})
output[-1][key] = value
The problem with the above code is that it does not maintain the list of lists and mixes up the OID field with other data items, I’d like a dict starting from each OID.
Any help would be greatly appreciated 🙂
Answers:
The problem is that you append a new dict to output
every time that the first element of output
has the key
you’re looking for. This causes your code to fail, because after the first list in splitted
has been processed, the first element of output
looks like this:
{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERPOOL',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'}
and all key
values you will see henceforth already exist in said element.
What you actually want to do is to add a new dict every time you encounter a new list in splitted
.
output = []
for l in splitted:
output.append(dict())
for key_value in l:
key, value = key_value.split(':', 1)
output[-1][key] = value
And now you get what you expected:
[{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERLAND',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'},
{'OID': 'XXXXXXXXXXX2',
' Name': '30',
'street': 'DA PLACE',
'town': 'PERTH',
'postcode': 'PH1 2DD',
'Name': '30'}]
While I have your attention:
dict
s are an unordered data type in python (or ordered by insertion-order), so "mixes up the OID field with other data items" isn’t really a thing. You wanted to create a single dict with all those keys, but you ended up creating a bunch of dicts, each with one key (after the first one)
list
is a built-in class in python, so creating a variable called list
shadows this class. You shouldn’t do this, because later you might encounter errors if you want to use the list
class.
- Debugging is a crucial skill for a programmer to have. I encourage you to take a look at these links: How to debug small programs.
|
What is a debugger and how can it help me diagnose problems? You can use a debugger to step through your code and observe how each statement affects the state of your program, and this helps you figure out where you’re going wrong.
You could also try to use dictionary comprehension:
[{x.split(":")[0]:x.split(":")[1] for x in splitted[0]},
{x.split(":")[0]:x.split(":")[1] for x in splitted[1]}]
The output is:
[{'OID': 'XXXXXXXXXXX1', ' street': 'THE ROAD', 'town': 'NEVERLAND', 'postcode': 'M1 2DD', 'Name': 'SOMEHWERE', 'street': 'THE ROAD'}, {'OID': 'XXXXXXXXXXX2', ' Name': '30', 'street': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD', 'Name': '30'}]
Make it sense for you?:
splitted = [['OID:XXXXXXXXXXX1',
' street:THE ROAD',
'town:NEVERPOOL',
'postcode:M1 2DD',
'Name:SOMEHWERE',
'street:THE ROAD',
'town:NEVERLAND',
'postcode:M1 2DD'],
['OID:XXXXXXXXXXX2',
' Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD',
'Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD']]
output = []
for i in range(len(splitted)):
output.append(dict())
for j in splitted[i]:
k,v = j.split(':')
output[i][k] = v
the output is:
[{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERLAND',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'},
{'OID': 'XXXXXXXXXXX2',
' Name': '30',
'street': 'DA PLACE',
'town': 'PERTH',
'postcode': 'PH1 2DD',
'Name': '30'}]
I have a list of lists like so:
splitted = [['OID:XXXXXXXXXXX1',
' street:THE ROAD',
'town:NEVERPOOL',
'postcode:M1 2DD',
'Name:SOMEHWERE',
'street:THE ROAD',
'town:NEVERLAND',
'postcode:M1 2DD'],
['OID:XXXXXXXXXXX2',
' Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD',
'Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD']]
I’d like to convert these to key values pairs like so:
{'OID': 'XXXXXXXXXXX1', ' street': 'THE ROAD', 'town': 'NEVERPOOL', 'postcode': 'M1 2DD', 'Name': 'SOMEWHERE', 'street': 'THE ROAD', 'town': 'NEVERPOOL', 'postcode': 'M1 2DD'}, {'MPXN': 'XXXXXXXXXXX2', ' Name': '30', 'street': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD', 'primaryName': '30', 'street1': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD'}
I am unable to find a way online to convert a list of lists into key-value pairs as a dict to then be consumed by pandas. The purpose of this is to convert the dicts into a pandas DataFrame so I can then consume it and work with it in a tabular format
The code I have used thus far is here:
output = []
for list in splitted:
for key_value in list:
key, value = key_value.split(':', 1)
if not output or key in output[0]:
output.append({})
output[-1][key] = value
The problem with the above code is that it does not maintain the list of lists and mixes up the OID field with other data items, I’d like a dict starting from each OID.
Any help would be greatly appreciated 🙂
The problem is that you append a new dict to output
every time that the first element of output
has the key
you’re looking for. This causes your code to fail, because after the first list in splitted
has been processed, the first element of output
looks like this:
{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERPOOL',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'}
and all key
values you will see henceforth already exist in said element.
What you actually want to do is to add a new dict every time you encounter a new list in splitted
.
output = []
for l in splitted:
output.append(dict())
for key_value in l:
key, value = key_value.split(':', 1)
output[-1][key] = value
And now you get what you expected:
[{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERLAND',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'},
{'OID': 'XXXXXXXXXXX2',
' Name': '30',
'street': 'DA PLACE',
'town': 'PERTH',
'postcode': 'PH1 2DD',
'Name': '30'}]
While I have your attention:
dict
s are an unordered data type in python (or ordered by insertion-order), so "mixes up the OID field with other data items" isn’t really a thing. You wanted to create a single dict with all those keys, but you ended up creating a bunch of dicts, each with one key (after the first one)list
is a built-in class in python, so creating a variable calledlist
shadows this class. You shouldn’t do this, because later you might encounter errors if you want to use thelist
class.- Debugging is a crucial skill for a programmer to have. I encourage you to take a look at these links: How to debug small programs.
|
What is a debugger and how can it help me diagnose problems? You can use a debugger to step through your code and observe how each statement affects the state of your program, and this helps you figure out where you’re going wrong.
You could also try to use dictionary comprehension:
[{x.split(":")[0]:x.split(":")[1] for x in splitted[0]},
{x.split(":")[0]:x.split(":")[1] for x in splitted[1]}]
The output is:
[{'OID': 'XXXXXXXXXXX1', ' street': 'THE ROAD', 'town': 'NEVERLAND', 'postcode': 'M1 2DD', 'Name': 'SOMEHWERE', 'street': 'THE ROAD'}, {'OID': 'XXXXXXXXXXX2', ' Name': '30', 'street': 'DA PLACE', 'town': 'PERTH', 'postcode': 'PH1 2DD', 'Name': '30'}]
Make it sense for you?:
splitted = [['OID:XXXXXXXXXXX1',
' street:THE ROAD',
'town:NEVERPOOL',
'postcode:M1 2DD',
'Name:SOMEHWERE',
'street:THE ROAD',
'town:NEVERLAND',
'postcode:M1 2DD'],
['OID:XXXXXXXXXXX2',
' Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD',
'Name:30',
'street:DA PLACE',
'town:PERTH',
'postcode:PH1 2DD']]
output = []
for i in range(len(splitted)):
output.append(dict())
for j in splitted[i]:
k,v = j.split(':')
output[i][k] = v
the output is:
[{'OID': 'XXXXXXXXXXX1',
' street': 'THE ROAD',
'town': 'NEVERLAND',
'postcode': 'M1 2DD',
'Name': 'SOMEHWERE',
'street': 'THE ROAD'},
{'OID': 'XXXXXXXXXXX2',
' Name': '30',
'street': 'DA PLACE',
'town': 'PERTH',
'postcode': 'PH1 2DD',
'Name': '30'}]