Skip to content Skip to sidebar Skip to footer

Appending To List - None Result

Data: lis_t = [['q', 'w', 'e'],['r', 't', 'y']] Expected Outcome: lis_t = [['q', 'w', 'e', 'u'],['r', 't', 'y']] problem description: I am trying to append to the list above how

Solution 1:

do this

lis_t = [['q', 'w', 'e'],['r', 't', 'y']]
lis_t[0].append('u')
print(lis_t)

Solution 2:

lis_t[0].append('u') this returns None value and then you assigning this to lis_t[0] that's why you are getting None value

lis_t = [['q', 'w', 'e'],['r', 't', 'y']]
lis_t[0].append('u')
print(lis_t)

Solution 3:

Python function always return value when invoked. If no return value provided it will return None.

python list.append() is function which have no return value so it will be returning None when invoked.

what you are doing is assigning the list.append() method invocation to lis_t[0]

so the correct way in your case is.

lis_t[0].append('u')

Solution 4:

apart from

list_t[0].append('u')

you can do (if you like to use the '+')

list_t[0] += ['u']

or

list_t[0] = list_t[0] + ['u']

The result is always this

print(list_t[0])

>>> ['q', 'w', 'e', 'u']

Post a Comment for "Appending To List - None Result"