Filter Data in List, Dictionary and Set

It’s a common operation to filter elements in list, dictionary and set.

For loop

data = [-1, 2, 3, -4, 5]
# filter out the negatives

res = []

for x in data:
    if x >= 0:
        res.append(x)

This is the least pythonic way.

List/Dictionary/Set Comprehension

Can use python’s list/dictionary/set comprehensions

List

First create a dummy data, using random library

from random import randint

data = [randint(-10, 10) for _ in range(10)]
res_lc = [x for x in data if x >= 0]

Dictionary

Create a dictionary data:

data_dict = {"std_"+str(i):randint(0,100) for i in range(20)}
res_dict = {k:v for k,v in data_dict.items() if v >= 60}

Set

res_set = {v for v in data_set if v >= 0}

Filter function

Use the built-in filter function, use lambda for filtering criteria

List

res = list(filter(lambda x: x>=0, data))

Filter will return a generator, need to use list to create a list

Dict

A very lengthy way

res_dict = {k:data_dict[k] for k in filter(lambda k: data_dict[k]>=60, data_dict)}

A much better solution

res_dict = dict(filter(lambda x: x[1]>=60, data_res.items()))

   Reprint policy


《Filter Data in List, Dictionary and Set》 by Isaac Zhou is licensed under a Creative Commons Attribution 4.0 International License
  TOC