0

I have a function that returns a list:

def get_all_newids() -> list:
    return list(group_dict.values())

When I'm trying to append this list on assignment it assigns None:

grpids = get_all_newids().append('42')  # None

If I append after the assignment everything works as expected:

grpids = get_all_newids()
grpids.append('42')  #  ['33', '42']

Just wondering why exactly this is happening. And is there any way to do this in one-liner?

khelwood
  • 55,782
  • 14
  • 81
  • 108
F1rstAIDs
  • 33
  • 1
  • 5

2 Answers2

2

You can do it in one line:

grpids = get_all_newids() + ['42']
eumiro
  • 207,213
  • 34
  • 299
  • 261
1

@eumiro provided a solution, however this was left unanswered:

Just wondering why exactly this is happening.

Because it's idiomatic (though not universal) for "mutating" methods to return None in Python when there is not a very good reason for them to return something (e.g. pop would be much less useful if it did not return the value it pops).

append adds an item to a list, and returns nothing because there is no return value intrinsic to appending. Likewise extend, insert, remove, reverse or sort.

Masklinn
  • 34,759
  • 3
  • 38
  • 57