Extending a 1-element list with another valid list returning NoneType

43 Views Asked by At

I have a list I generate like this:

cols = [td['data-stat'] for td in rows[0].find_all("td")]

cols contains: ['age','team_id','lg_id','pos','g','gs','mp','fg', ...] as it should. However, when I insert "season" to the beginning OR extend ["season"] with cols, I suddenly get NoneType:

new_cols = ["season"].extend(cols)

Any ideas?

1

There are 1 best solutions below

0
Red On

The list.extend() method doesn't return a value, it alters an existing one.

If you want a separate list that contains every element in the cols list, along with the "extend" string, use the list.copy() method to duplicate the list, and use the list.extend() method on the new list:

new_cols = cols.copy() 
new_cols.extend(["season"])

If you do not intend to keep the old list, then you can directly use the list.extend() method on the old list:

cols.extend(["extend"])