2
lebel_months = []
members_exps = []  

for member in team.members.all():
    qs = Profile.objects.expenses_per_month(member.user)
    clr = color_picker()
    member_qs = []
    for exp in qs:
        user = get_object_or_404(User, id=exp.user_id)
        month = exp.month
        summary = exp.sum
        if month not in lebel_months and month is not None:
            lebel_months.append(month)
        if month is not None and summary is not None:                
            member_qs.append({
                'user': user.username, 
                'clr': clr, 
                'month': month, 
                'summary': summary})
    if member_qs:
        members_exps.append(member_qs)

print(members_exps[0])
print(members_exps.first())

outputs: with [0] everything works

[{'user': 'serj', 'clr': '#71CABC', 'month': datetime.datetime(2020, 3, 1, 0, 0, tzinfo=<UTC>), 
'summary': 128400}, {'user': 'serj', 'clr': '#71CABC', 'month': datetime.datetime(2020, 4, 1, 0, 0, 
tzinfo=<UTC>), 'summary': 53500}]

with first()

AttributeError: 'list' object has no attribute 'first'

First print statement works. Second print statement catch an error. What am I doing wrong?

2 Answers2

0

As the error says, the members_exps variable is a list, and in python lists do not have a first() attribute/function. first only works for dataframes.

0

Firstly, members_exps = [] declares List type.

Lists/Arrays don't have a first() method. There's a list of available methods: https://www.w3schools.com/python/python_ref_list.asp

If you want to get the first item of the List you might be interested to read the following thread: Python idiom to return first item or None

Viktor Born
  • 186
  • 1
  • 5
  • > Lists/Arrays don't have a first() method. Indeed, because why would you ever want such a method? Perhaps in Python 4; after all, Python finally got a switch statement in 3.10 after 30 years. – michael_teter Mar 31 '22 at 08:29