Friday 4 December 2015

Python: list extend: append elements of given list

Python provides ‘extend’ method to append all the items in the given list.
test.py

evenNumbers=[2, 4, 6]
oddNumbers=[1, 3, 5]
numbers=[]

numbers.extend(evenNumbers)
print(numbers)

numbers.extend(oddNumbers)
print(numbers)

$ python3 test.py
[2, 4, 6]
[2, 4, 6, 1, 3, 5]





Previous                                                 Next                                                 Home

No comments:

Post a Comment