Extending a list in python can be done is following ways:
1. Using append() function: We can append at the end of the list by using append() function. For appending any single value to the list or appending a list to the list, the syntax stays the same. But we can only append a single value at a time using append() function
[10, 12, 13, 17, 20, 22]
2. Using ‘+’ operator: We can add values by using the “+” operator. We can use  to add any number of values to the list. Adding multiple values can be done by using ‘, ‘ values.
[10, 12, 13, 17, 20, 30, 40]
3. Using slicing: Using slicing in python, single or multiple values can be added to a list.
a[:0] = [x, y, z…]
Here a is the list in which the values(x, y, z..) are to be added. In this method the values are appended to the front of the list.
[40, 50, 30, 10, 12, 13, 17]
4.Using chain(): Using chain() iterator function, we can extend a list by the syntax:
list(chain(a, [x, y, z..]))
Here a is the list in which the values(x, y, z..) are to be added. In this method the values are appended to the end of the list.
[10, 20, 30, 40, 50, 60]
5. Using Extend
[10, 12, 13, 17, 30, 40]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Extending and customizing django-allauth
- Creating custom user model API extending AbstractUser in Django
- Python | Ways to split a string in different ways
- Different ways to clear a list in Python
- Python | Multiply all numbers in the list (4 different ways)
- Different ways to access Instance Variable in Python
- Reverse string in Python (5 different ways)
- Print lists in Python (4 Different Ways)
- Python | Different ways to kill a Thread
- Different ways to Invert the Binary bits in Python
- Different ways to convert a Python dictionary to a NumPy array
- Python | Ways to sum list of lists and return sum list
- Python | Ways to Convert a 3D list into a 2D list
- PyQt5 – Different padding size at different edge of Label
- PyQt5 - Setting different toolTip to different item of ComboBox
- Different ways to import csv file in Pandas
- Different ways to iterate over rows in Pandas Dataframe
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Different ways of sorting Dictionary by Values and Reverse sorting by values
- Different ways to create Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai