extend() in Python

Configurare noua (How To)

Situatie

extend() iterates over its argument and adding each element to the list and extending the list. The length of the list increases by number of elements in it’s argument.

Solutie

Pasi de urmat

 

my_list = [‘this’, ‘is’, ‘a’]
my_list.extend(‘test’)
print my_list

 

Output:

[‘this’, ‘is’, ‘a’, ‘t’, ‘e’, ‘s’, ‘t’]

 

my_list = [‘this’, ‘is’, ‘a’, ‘t’, ‘e’, ‘s’, ‘t’]
another_list = [1, 0, 3, 2]
my_list.extend(another_list)
print my_list

 

Output:

[‘this’, ‘is’, ‘a’, ‘t’, ‘e’, ‘s’, ‘t’, 6, 0, 4, 1]

Tip solutie

Permanent

Voteaza

(1 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?