0

I've created an array for my output by doing this:

for i in nameList
    test_array.append({'Name': i, 'Email': memberMail, 'Department': memberDepartment})

However, later in the code, I must remove designated values in my test_array depending on their email. After that, I can easily print out what I need to my csv file.

How do I delete a specific entry from this sort of dictionary list?

For those curious, when I print the array currently it looks like this:

[{'Department': 'Public Works', 'Email': '[email protected]', 'Name': 'Joe'}, {'Department': 'Infrastructure', 'Email': '[email protected]', 'Name': 'Bob'}, {'Department': 'IT', 'Email': '[email protected]', 'Name': 'Suzanne'}]
0

4 Answers 4

1

For when you not want to modify test_array

filtered_test_array = filter(lambda entry: entry['Email'] != '[email protected]', test_array)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

 for i in range(0,len(nameList)):
        if nameList[i]['Email'] == '[email protected]" :
             index = i;

    del nameList[index];

Comments

0

Try like this.

list_ = [{'Department': 'Public Works', 'Email': '[email protected]', 'Name': 'Joe'}, {'Department': 'Infrastructure', 'Email': '[email protected]', 'Name': 'Bob'}, {'Department': 'IT', 'Email': '[email protected]', 'Name': 'Suzanne'}]
for val in list_:
    if val['Email'] == '[email protected]':
        list_.remove(val)

Result

[{'Department': 'Infrastructure', 'Email': '[email protected]', 'Name': 'Bob'},
 {'Department': 'IT', 'Email': '[email protected]', 'Name': 'Suzanne'}]

Comments

0

You can iterate over the list and delete with respect to a unique value or a key. Something like this:

for entry in test_array:
    if entry['Email'] == '[email protected]':
        test_array.remove(entry)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.