Home > database >  Python - Filter and Lambda Beginner Question
Python - Filter and Lambda Beginner Question

Time:07-09

I'm learning Python through Colt Steele Modern Python Bootcamp. I'm now on filter section and I'm having trouble understanding this or I guess I have a trouble understanding lists and dictionaries.

In video it shows a list of dictionaries

users = [
{"username":"samuel", "tweets": ["blabla"]},
{"username":"test", "tweets": []}
]

Then he goes to separate inactive users, the ones with empty key tweets by:

inactive_users = list(filter(lambda u:len(u["tweets"]) == 0, users)

This works of course, however just for the sake of learning, I tried to apply it with a for loop but I'm unsuccessful.

Because this is what I was trying to do

users = [
    {"username": "samuel", "tweets": []},
    {"username":"toni","tweets": ["blabla"]},
    {"username": "tata", "tweets": []},
    {"username":"hHh","tweets": ["abba"]},
    {"username": "SAEl", "tweets": []},
    {"username":"brte","tweets": ["abba"]}
]
inaktiv = []
index = 0
for u in users:
    if len(users[index]["tweets"]) == 0:
        inaktiv.append(u)
        index  = 1
print(inaktiv)

My question is... How can this be achieved with a for loop because I can't figure it out. I tried different variations of for loop.

All I get is an empty []

Thanks in advance

CodePudding user response:

The correct way would be:

inaktiv = []
for u in users:
    if len(u["tweets"]) == 0:
        inaktiv.append(u)

If you come, for example, from a C background and you're very used to for cycles with indexes to access array elements, then you will probably find this solution a bit easier to understand (but it's not pythonic, nor the recommended one):

inaktiv = []
for index in range(len(users)):
    if len(users[index]["tweets"]) == 0:
        inaktiv.append(users[index])

Instead, using a list comprehension would be much better:

inaktiv = [u for u in users if len(u["tweets"]) == 0]

Also, remember that len(u["tweets"]) == 0 has the same effect (in this case) as not u["tweets"], so you can shorten the whole code a bit:

inaktiv = [u for u in users if not u["tweets"]]

CodePudding user response:

The first problem I detect in your code is that your index increase is under your if condition. This means that if the first person in users is active the index will not increase. So you will have an empty list of inactive people.

CodePudding user response:

I think you wanted to achieve something like this:

users = [
    {"username": "samuel", "tweets": []},
    {"username":"toni","tweets": ["blabla"]},
    {"username": "tata", "tweets": []},
    {"username":"hHh","tweets": ["abba"]},
    {"username": "SAEl", "tweets": []},
    {"username":"brte","tweets": ["abba"]}
]
inactive = []
for user in users:
    if len(user['tweets']) == 0: # also you can use: if not user['tweets']
        inactive.append(user)
print(inactive)

It is worth noting that you were close to the goal. The problem with your code was that you were only incrementing the index if the array was empty. The problem is fixed like this:

users = [
    {"username": "samuel", "tweets": []},
    {"username":"toni","tweets": ["blabla"]},
    {"username": "tata", "tweets": []},
    {"username":"hHh","tweets": ["abba"]},
    {"username": "SAEl", "tweets": []},
    {"username":"brte","tweets": ["abba"]}
]
inaktiv = []
index = 0
for u in users:
    if len(users[index]["tweets"]) == 0:
        inaktiv.append(u)
    index  = 1
print(inaktiv)
  • Related