0

I have a list of lists that looks like this:

[[['1',
    '1@1`']],
  [['2', '[email protected]']],
  [['3', '[email protected]']],
  [['4', '[email protected]']],
  [['5', '[email protected]']],
  [['6', '6@6']],
  [['7', '7@7']],
  [['8', '8@8']],
  [['8.5', '[email protected]']],
  [['9', '9@9']],
  [['10', '10@10']],
  [['11', '11@11']],
  [['12', '12@12']],
  [['13', '[email protected]']],
  [['14', '[email protected]']],
  [['15', '[email protected]']],
  [['16', '[email protected]']],
  [['17', '[email protected]']],
  [['[email protected]', '18']],
  [['19', '[email protected]']]]

is there anyway I can clean up the list by making it into a dictionary object like so:

[{id:1,email:1@1},{id:2,email:[email protected]}]

Ideally if there are any emails in the id spot they flipped to the email spot?

2
  • yeah, sure there is a way. Iterate over your list, create a dict from the elements in your list (although, why it's a bunch of single-element lists is weird, but you can easily just handle that). Commented Jan 19, 2019 at 1:00
  • "Ideally if there are any emails in the id spot they flipped to the email spot?" This might be a little bit more tricky. But maybe just checking if @ is in it? Commented Jan 19, 2019 at 1:01

1 Answer 1

2

You can use a list comprehension:

In [1]: mylist = [[['1',
   ...:     '1@1`']],
   ...:   [['2', '[email protected]']],
   ...:   [['3', '[email protected]']],
   ...:   [['4', '[email protected]']],
   ...:   [['5', '[email protected]']],
   ...:   [['6', '6@6']],
   ...:   [['7', '7@7']],
   ...:   [['8', '8@8']],
   ...:   [['8.5', '[email protected]']],
   ...:   [['9', '9@9']],
   ...:   [['10', '10@10']],
   ...:   [['11', '11@11']],
   ...:   [['12', '12@12']],
   ...:   [['13', '[email protected]']],
   ...:   [['14', '[email protected]']],
   ...:   [['15', '[email protected]']],
   ...:   [['16', '[email protected]']],
   ...:   [['17', '[email protected]']],
   ...:   [['[email protected]', '18']],
   ...:   [['19', '[email protected]']]]

In [2]: [{'id': i, 'email': e} for i, e in (pair[0] if '@' not in pair[0][0] else reversed(pair[0]) for pair in mylist)]
Out[2]:
[{'id': '1', 'email': '1@1`'},
 {'id': '2', 'email': '[email protected]'},
 {'id': '3', 'email': '[email protected]'},
 {'id': '4', 'email': '[email protected]'},
 {'id': '5', 'email': '[email protected]'},
 {'id': '6', 'email': '6@6'},
 {'id': '7', 'email': '7@7'},
 {'id': '8', 'email': '8@8'},
 {'id': '8.5', 'email': '[email protected]'},
 {'id': '9', 'email': '9@9'},
 {'id': '10', 'email': '10@10'},
 {'id': '11', 'email': '11@11'},
 {'id': '12', 'email': '12@12'},
 {'id': '13', 'email': '[email protected]'},
 {'id': '14', 'email': '[email protected]'},
 {'id': '15', 'email': '[email protected]'},
 {'id': '16', 'email': '[email protected]'},
 {'id': '17', 'email': '[email protected]'},
 {'id': '18', 'email': '[email protected]'},
 {'id': '19', 'email': '[email protected]'}]

If you have arbitrary nesting, you can try this:

def flatten(lst):
    for sub in lst:
        if isinstance(sub, list):
            yield from flatten(sub)
        else:
            yield sub

[{'id': i, 'email': e} for i, e in (pair if '@' not in pair[0] else reversed(pair) for pair in zip(*[flatten(mylist)]*2))]
Sign up to request clarification or add additional context in comments.

9 Comments

thank you for the response. I am getting error : KeyError: 0. Not sure what this means.
@RustyShackleford Can you confirm that your input is a list of lists? That KeyError likely occurred because you have a list of dictionaries.
confirmed its a list with 20 other lists inside of it.
@RustyShackleford Can you share the full error via Pastebin or similar? Also, can you double check that each item is wrapped in two lists e.g. the whole list is a 3D structure.
I apologize used the wrong list but got this error with right list :ValueError: not enough values to unpack (expected 2, got 1). Confirmed each item is wrapped in two lists.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.