Is it possible to add more than one item to a list at once?

Asked

Viewed 1,296 times

4

I would like to know if it is possible to add more than one element to a list, for example:

a=[]
a.append('abóbora', 'banana', 'maçã')

or it will be necessary to use three lines, for example:

a=[]
a.append('abóbora')
a.append('banana')
a.append('maçã')

answer me, please answer me.

2 answers

4


With the method .append you will not be able to place a sequence of values inside a list. The correct method to perform the input of a sequence is the .extend, soon it will be like this:

lista = []
lista.extend(('abóbora', 'banana', 'maçã'))
print(lista)

exit:

['abóbora', 'banana', 'maçã']

2

You can use the operator += to concatenate one list into another:

a = []
a += ['abóbora', 'banana', 'maçã']
print(a)

Exit:

['abóbora', 'banana', 'maçã']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.