Python list is one of the most popular data types.Python Lists are used to store multiple items in a single variable. List items are ordered, changeable, and allow duplicate values. In the list, you can add elements, access it using indexing, change the elements, and remove the elements.
List items are indexed, the first item has index [0], the second item has index [1] etc.
You can also checkout other python list tutorials:
- Check Element Exist in List
- How to Filter a List in Python
- Python List Example And Methods
- How To Compare Python Two Lists
- How to Concatenate Two List in Python
- How To Match String Item into List Python
- Python String join Example
Python join list
We’ll join a list in Python using several ways. You can use one of the following three ways.
- Using + operator to join two lists.
- Appending one list item to another using the list append() method.
- Using the list extend() method to add one list’s elements to another.
Using + operator to join two lists
Python is providing + operator to join lists, It’s a concatenation operator that joins two lists.
fnames = ["Ajay", "Adam", "Sukhvinder", "Parvez"] lnames = ["Kumar", "Joe", "Singh", "Alam"] joinedList = fnames + lnames print(joinedList)
Output:['Ajay', 'Adam', 'Sukhvinder', 'Parvez', 'Kumar', 'Joe', 'Singh', 'Alam']
You can see that the elements of both lists are merged into one.
Using the list append() method
Python List append()
method is used to add a single element to the existing list. You can add a single element or multiple items into lists using loop.
fnames = ["Ajay", "Adam", "Sukhvinder", "Parvez"] fnames.append("Kumar") print(fnames) lnames = ["Joe", "Singh", "Alam"] for lname in lnames: fnames.append(lname) print(fnames)
Output:
['Ajay', 'Adam', 'Sukhvinder', 'Parvez', 'Kumar'] ['Ajay', 'Adam', 'Sukhvinder', 'Parvez', 'Kumar', 'Joe', 'Singh', 'Alam']
Using the list extend() method to join lists
Python extend()
function is used to add list items at the end of the current list.
fnames = ["Ajay", "Adam", "Sukhvinder", "Parvez"] lnames = ["Joe"] fnames.extend(lnames) print(fnames)
Output:
['Ajay', 'Adam', 'Sukhvinder', 'Parvez', 'Joe']
Conclusion:
We have covered all the ways to join lists into python. You can an element, list of items, and joins one list to another python list.