Python Join List: How to Join List in Python
Python list is one of the most popular data types because it has ordered, changeable, and allows duplicate values. In the list, you can add elements, access it using indexing, change the elements, and remove the elements.
Python join list
To join a list in Python, there are several ways in Python.
- 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 lists elements to another.
Using + operator to join two lists
The+ operatoris a concatenation operator that joins two lists.
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] joinedList = houses + wizards print[joinedList]Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']You can see that the elements of both lists are merged into one.
Using the list append[] method
Python List append[]method adds a single element to the existing list. So, we need a for loop to join two lists into one.
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] for element in wizards: houses.append[element] print[houses]Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']Using the list extend[] method to join lists
Python extend[] function adds the specified list elements [or any iterable] to the end of the current list.
Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']Python join[]
To convert the Python list to a string, use the join[] method. Python join[] is an inbuilt function that returns the string concatenated with an iterable element. It takes iterable as an argument and returns the joined elements. Python List is iterable so that we can pass the list to the join[] function.
Now, there is one catch that needs to be considered, and that is the list should consist of strings and not integers because the join[] function will apply to strings and not integers.
Example of join[] in Python
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] csvOfHouses = ",".join[houses] print[csvOfHouses]Output
Griffindor,Salazar,Rowena,HelgaThe output is a comma-separated string. In this example, we used a commaas a delimiter.
Joining the list of multiple data-types
We can not join list items having multiple data types using the join[] function because the join[] function cannot work other than string data types.
houses = ["Griffindor", 10, True, "1921"] delimiter = ',' hp = delimiter.join[houses] print[f'String: {hp}']Output
TypeError: sequence item 1: expected str instance, int foundIf you apply the join[] function to a list of integers, you will get TypeError: sequence item 0: expected str instance, int found.
That is it for this tutorial.
Python list is one of the most popular data types because it has ordered, changeable, and allows duplicate values. In the list, you can add elements, access it using indexing, change the elements, and remove the elements.
Python join list
To join a list in Python, there are several ways in Python.
- 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 lists elements to another.
Using + operator to join two lists
The+ operatoris a concatenation operator that joins two lists.
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] joinedList = houses + wizards print[joinedList]Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']You can see that the elements of both lists are merged into one.
Using the list append[] method
Python List append[]method adds a single element to the existing list. So, we need a for loop to join two lists into one.
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] for element in wizards: houses.append[element] print[houses]Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']Using the list extend[] method to join lists
Python extend[] function adds the specified list elements [or any iterable] to the end of the current list.
Output
['Griffindor', 'Salazar', 'Rowena', 'Helga', 'Harry', 'Draco', 'Suzan', 'Cedric']Python join[]
To convert the Python list to a string, use the join[] method. Python join[] is an inbuilt function that returns the string concatenated with an iterable element. It takes iterable as an argument and returns the joined elements. Python List is iterable so that we can pass the list to the join[] function.
Now, there is one catch that needs to be considered, and that is the list should consist of strings and not integers because the join[] function will apply to strings and not integers.
Example of join[] in Python
houses = ["Griffindor", "Salazar", "Rowena", "Helga"] csvOfHouses = ",".join[houses] print[csvOfHouses]Output
Griffindor,Salazar,Rowena,HelgaThe output is a comma-separated string. In this example, we used a commaas a delimiter.
Joining the list of multiple data-types
We can not join list items having multiple data types using the join[] function because the join[] function cannot work other than string data types.
houses = ["Griffindor", 10, True, "1921"] delimiter = ',' hp = delimiter.join[houses] print[f'String: {hp}']Output
TypeError: sequence item 1: expected str instance, int foundIf you apply the join[] function to a list of integers, you will get TypeError: sequence item 0: expected str instance, int found.
That is it for this tutorial.