Use the built-in extend()
method from list
to add all the items of an iterable to the end of a list.
The method doesn’t return any value, it alters the original list.
Lists, sets and tuples are examples of iterables.
In this example, car_brands_list
is initialized with two items.
car_brands_list
is then extended with the items of the list car_brands_list_two
.
After that car_brands_list
is extended with the items of the tuple car_brands_tuple
.
Finally, car_brands_list
is extended with the items of the set car_brands_set
.
car_brands_list = ['mclaren', 'ferrari']
car_brands_list_two = ['honda', 'ford']
car_brands_tuple = ('toyota', 'suzuki')
car_brands_set = {'kia', 'bmw'}
print(car_brands_list)
car_brands_list.extend(car_brands_list_two)
print(car_brands_list)
car_brands_list.extend(car_brands_tuple)
print(car_brands_list)
car_brands_list.extend(car_brands_set)
print(car_brands_list)
['mclaren', 'ferrari']
['mclaren', 'ferrari', 'honda', 'ford']
['mclaren', 'ferrari', 'honda', 'ford', 'toyota', 'suzuki']
['mclaren', 'ferrari', 'honda', 'ford', 'toyota', 'suzuki', 'bmw', 'kia']