How to convert List of Dictionary to simple 2D list in python to perform the specific task?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to convert List of Dictionary to simple 2D list in python to perform the specific task?



I have a list which looks something like this :


myList = [{'email_address': 'abc@gmail.com', 'status': 'subscribed'}, {'email_address': 'def@live.com', 'status': 'subscribed'}, {'email_address': 'ghi@gmail.in', 'status': 'pending'}, {'email_address': 'jkl@gmail.com', 'status': 'subscribed'}, {'email_address': 'mno@gmail.com', 'status': 'pending'}, {'email_address': 'pqr@yahoo.com', 'status': 'pending'}, {'email_address': 'stu@gmail.com', 'status': 'pending'}, {'email_address': 'vwx@y.com', 'status': 'pending'}, {'email_address': 'yz@z.com', 'status': 'pending'}]



I want to convert the list of the dictionary into a simple list as I don't require the 'email_address' and 'status' labels. I want the list to look something like this:


newList = [['abc@gmail.com', 'subscribed'], ['def@live.com', 'subscribed'], ['ghi@gmail.in', 'pending'], ['jkl@gmail.com', 'subscribed'], and so on...]



How do I convert the list as described?



And also after converting to this list how do I store the email addresses with 'subscribed' as the second field in one list and 'pending' as the second field in another list. For example:


subscribedList = ['abc@gmail.com', 'def@gmail.com', 'jkl@gmail.com' and so on...]
pendingList = ['ghi@gmail.com', 'mno@gmail.com', 'pqr@gmail.com' and so on...]



As I'm new to python I find conversions from list difficult please do help me out.



Note: 1. The myList is coming from an API and cannot be changed.


myList


abc@gmail.com




1 Answer
1



Use a list comprehension and the dict.values to get only values.


dict.values



Ex:


myList = [{'email_address': 'abc@gmail.com', 'status': 'subscribed'}, {'email_address': 'def@live.com', 'status': 'subscribed'}, {'email_address': 'ghi@gmail.in', 'status': 'pending'}, {'email_address': 'jkl@gmail.com', 'status': 'subscribed'}, {'email_address': 'mno@gmail.com', 'status': 'pending'}, {'email_address': 'pqr@yahoo.com', 'status': 'pending'}, {'email_address': 'stu@gmail.com', 'status': 'pending'}, {'email_address': 'vwx@y.com', 'status': 'pending'}, {'email_address': 'yz@z.com', 'status': 'pending'}]
print( [i.values() for i in myList] )



Output:


[['subscribed', 'abc@gmail.com'], ['subscribed', 'def@live.com'], ['pending', 'ghi@gmail.in'], ['subscribed', 'jkl@gmail.com'], ['pending', 'mno@gmail.com'], ['pending', 'pqr@yahoo.com'], ['pending', 'stu@gmail.com'], ['pending', 'vwx@y.com'], ['pending', 'yz@z.com']]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived