Manoj Jahgirdar
Manoj Jahgirdar

Reputation: 172

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': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', '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 = [['[email protected]', 'subscribed'], ['[email protected]', 'subscribed'], ['[email protected]', 'pending'], ['[email protected]', '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 = ['[email protected]', '[email protected]', '[email protected]' and so on...]
pendingList = ['[email protected]', '[email protected]', '[email protected]' 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.

  1. I have used the dummy email addresses like [email protected] for illustration purpose only. The real Email addresses are different.

Upvotes: 0

Views: 45

Answers (2)

optimalic
optimalic

Reputation: 511

newList = [list(item.values()) for item in myList]
#>>>[['[email protected]', 'subscribed'], ['[email protected]', 'subscribed'], ['[email protected]', 'pending'], ['[email protected]', 'subscribed'], ['[email protected]', 'pending'], ['[email protected]', 'pending'], ['[email protected]', 'pending'], ['[email protected]', 'pending'], ['[email protected]', 'pending']]

subscribedList = [item.get("email_address") for item in myList if item.get("status") == "subscribed"]
#>>>['[email protected]', '[email protected]', '[email protected]']

pendingList = [item.get("email_address") for item in myList if item.get("status") == "pending"]
#>>>['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

Upvotes: 1

Rakesh
Rakesh

Reputation: 82815

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

Ex:

myList = [{'email_address': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'subscribed'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}, {'email_address': '[email protected]', 'status': 'pending'}]
print( [i.values() for i in myList] )

Output:

[['subscribed', '[email protected]'], ['subscribed', '[email protected]'], ['pending', '[email protected]'], ['subscribed', '[email protected]'], ['pending', '[email protected]'], ['pending', '[email protected]'], ['pending', '[email protected]'], ['pending', '[email protected]'], ['pending', '[email protected]']]

2nd Que

for i in myList:
    if i["status"] == 'subscribed':
        subscribedList.append(i["email_address"])
    else:
        pendingList.append(i["email_address"])
print(subscribedList)
print(pendingList)

Output:

['[email protected]', '[email protected]', '[email protected]']
['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

Upvotes: 1

Related Questions