Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .append for several string items at a time

This is probably a basic question for most of you but I can't find any specific info on by googling, nor in any of the previously asked questions:

I would like to know whether it is possible to append several items to a list already containing one item by using .append and then get the number output of all of them using len(). When I enter my code as follows:

bag.append('suit', 'shoes', 'socks')

I get the following error:

TypeError: append() takes exactly one argument (3 given)

I have tried double parentheses as follows:

bag.append(('suit', 'shoes', 'socks))

but this results in len(bag) telling me that there are only 2 items in bag (i.e. the original one & (suit, shoes, socks). The number aimed for is 4 (i.e. original item + suit + shoes + socks).

The only method I have used that has successfully accomplished this is as follows:

bag.append('suit')
bag.append('shoes')
bag.append('socks')

When running len(bag) on this, I get the correct output for the 4 items separately, i.e. 4 (original item bag = [gloves] + 'suit' + 'shoes' + 'socks'.

like image 705
datalink Avatar asked Aug 08 '26 13:08

datalink


1 Answers

Rather than append, extend:

bag.extend(['suit', 'shoes', 'socks'])

See e.g. here.

like image 199
jonrsharpe Avatar answered Aug 11 '26 01:08

jonrsharpe