11

I have a list structure like this:

listpost =
[
   {
      "post_id":"01",
      "text":"abc",
      "time": datetime.datetime(2021, 8, 5, 15, 53, 19),
      "type":"normal",
   },
   {
      "post_id":"02",
      "text":"nothing",
      "time":datetime.datetime(2021, 8, 5, 15, 53, 19),
      "type":"normal",
   }
]

I want to filter the list by text in [text] key if only the [text] has "abc"

so the example will look like this

listpost =
[
   {
      "post_id":"01",
      "text":"abc",
      "time": datetime.datetime(2021, 8, 5, 15, 53, 19),
      "type":"normal",
   }
]

My code:

from facebook_scraper import get_posts

listposts = []

for post in get_posts("myhealthkkm", pages=1):
    listposts.append(post)
print(listposts)


1
  • What is your question? Why did you not use an if statement to only append post if post['text'] is equal to 'abc'? Commented Aug 5, 2021 at 9:22

5 Answers 5

13

Since you specifically asked about filtering the list you have, you can use filter builtin with lambda to filter out the elements from the list.

>>> list(filter(lambda x: x.get('text', '')=='abc', listpost))

[{'post_id': '01', 'text': 'abc', 'time': datetime.datetime(2021, 8, 5, 15, 53, 19), 'type': 'normal'}]

But I'd recommend to filter it out upfront before actually appending it to the list, to avoid unnecessary computations due to the need to re-iterate the items i.e. appending only the items that match the criteria.

Something like this:

for post in get_posts("myhealthkkm", pages=1):
    if <post match the condition>:
        listposts.append(post)  # append the post
Sign up to request clarification or add additional context in comments.

Comments

10

simple like that:

filtered_list = [e for e in listpost if e['text'] == 'abc']

Comments

4

If you want to filter elements that are exactly "abc"

new_list = [el for el in listpost if el["text"]=="abc"]

If you want to keep elements that starts with "abc"

new_list = [el for el in listpost if el["text"].startswith("abc")]

If you want to keep elements that contains the "abc" substring

new_list = [el for el in listpost if "abc" in el["text"]]

Comments

2
 list(filter(lambda x: x['text']=='abc', listpost))

Hope this works

Comments

1

A good pythonic solution is already given, but here's another solution closer to what you were trying:

from facebook_scraper import get_posts

listposts = []
wanted = "abc"  # wanted post
for post in get_posts("myhealthkkm", pages=1):
    if t:=post.get("text", "") == wanted:
        # print("Found", t)
        listposts.append(post) 
    else:
        pass
        # print("Not found")
print(listposts)

4 Comments

What if i want to change the == to in ?. Cause i want to check if abc is contain inside the text key or not and not equal.
When i put contain like thisif wanted in post.get("text", ""): it will keep showing two same element instead of one
@johnny2000 the if wanted in post.get("text", ""): should work fine. In case of duplicates, you can always make the list a set and then back to list. I.e. s = set(current_list); new_list=list(s). This should only keep the unique elements.
Just now i tried it worked ! But after a couple times it should me TypeError: argument of type 'NoneType' is not iterable

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.