List join Python

Python

Python Join List: How to Join List in Python

By Krunal Last updated Sep 1, 2021 1

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.

  1. Using + operator to join two lists.
  2. Appending one list item to another using the list append[]method.
  3. 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.

houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] houses.extend[wizards] print[houses]

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,Helga

The 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 found

If 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.

  1. Using + operator to join two lists.
  2. Appending one list item to another using the list append[]method.
  3. 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.

houses = ["Griffindor", "Salazar", "Rowena", "Helga"] wizards = ["Harry", "Draco", "Suzan", "Cedric"] houses.extend[wizards] print[houses]

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,Helga

The 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 found

If 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.

Video liên quan

Chủ Đề