Is adding more than one element to Append possible?

Asked

Viewed 362 times

0

I can add more than one element to the function append?

For example:

convite.append("Carlos", "Junior") 
convite["Alberto", "Eduardo"]

2 answers

8


With the method append is not possible. If you look at documentation you will see that it accepts only one parameter:

list.append(x)

Add an item to the end of the list. Equivalent to a[len(a):] = [x].

But you can add more elements via the sum operator, +:

nomes = ['Anderson']
nomes += ['Carlos', 'Woss']

print(nomes) # ['Anderson', 'Carlos', 'Woss']

Or even do as one’s own append ago:

nomes = ['Anderson']
nomes[len(nomes):] = ['Carlos', 'Woss']

print(nomes) # ['Anderson', 'Carlos', 'Woss']

There is also the method extends, commented on this question:

1

His question indicates that he did not study the documentation as to the append method or the use of the language as a whole, specifically about loops. Anyway, you can use a for loop for that reason:

convite = list()
for i in ["Alberto", "Eduardo"]:
    convite.append(i)

print(convite)
>>> ["Alberto", "Eduardo"]

Browser other questions tagged

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