Python Join List

In this topic, we will discuss how we can join two or more lists with different functions of Python. Before going through the concepts, let's take a brief introduction to the Python List. A Python List is the collection of multiples items that are grouped in the same name. It can store different data types (integer, string, float, etc.) items inside a square bracket [], which is separated by a (,) comma.

Python Join List

Program to print the Python list

List.py

Output

Display the List1  ['A', 'B', 'C', 'D', 'E']
 Display the List2  [1, 2, 3, 4, 5]
 Display the List3  ['A', 1, 'C', 'E', 5, 8]

When we join two or more lists together in a Python program, it gives a joined lists. And this process is called composition or joining of lists.

Let's discuss the different ways to join two or more lists in Python:

  • Join lists in Python using the join() function and delimiters
  • Join a list in Python using the join() function without delimiters
  • Join two integers list in Python using map() function
  • Join two lists in Python using for loop and append() function
  • Join multiple lists in Python using itertools.chain() method
  • Join two lists in Python using (+) plus operator
  • Join two lists in Python using (*) multiply or asterisk operator
  • Join two lists in Python using extend() function

Join lists in Python using the join() function

A join() function is used to join an iterable list to another list, separated by specified delimiters such as comma, symbols, a hyphen, etc.

Syntax

str_name: It is the name of the delimiter that separates an iterable list.

iterable: It is the list that contains a set of elements and joins with a delimiter.

Return Value: It returns a concatenated list that is separated by specified delimiters.

Note: If the iterable list contains any non-string values or items, it throws a TypeError exception.

Program to join two lists using the join() function and delimiter

Join.py

Output

Display the concatenated List1 using join() function and delimiter Apple, Orange, Banana, Mango, Grapes
 Display the concatenated List2 using join() function and delimiter Sunday - Monday - Tuesday - Wednesday - Thursday

Program to join a list without using delimiter

Prog.py

Output

Display the elements of the List L1  ['j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't']
 Display the List without using delimiters j  a  v  a  t  p  o  i  n  t

Join two integers list using map() function

Integer list: It collects all integers in a list called the integer list, and we cannot join two integer lists in Python using the join() function. Therefore, we use a map() function that converts an integer list into a string. After that, we use a join() function to join map() function results with appropriate delimiters.

Syntax:

In the above syntax, a map() function has two parameters, list_name, and str. Where list_name is the name of integers list and str represents the string. A map() function converts the list_name into the string (str).

Program to use a map() function and join() function in list

Let's create a program to convert the given integers list into a string using the map() function and then join() function to join the list.

Convert.py

Output

Display the concatenated integers list using map() and join() function  1, 2, 3, 4, 5

Program to join two lists in Python using for loop and append() function

An append() function is used to sequentially add or join each element of an iterable list at the end of another list using for loop. Let's create a simple program to add elements of a list at the end of another list using the append() function.

Append.py

Output

Given List1  [1, 2, 3, 4, 5]
 Given List2  [5, 6, 7, 8, 9, 10]
 Display concatenation list using append() function  [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10]

Program to join multiple lists using itertools.chain() method

Let's create a simple program in Python to concatenate multiple lists using the chain() method by importing the itertools package.

New.py

Output

Display the first list  [1, 2, 3, 4, 5]
 Display the second list  [6, 7, 8, 9, 10]
 Display the third list  [11, 12, 13, 14, 15]
 Concatenated list in python using itertools.chain() method  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Program to join two lists using + operator

Let's consider an example to join two lists in Python using the (+) plus operator.

Mypro.py

Output

Join two list of characters in Python using + operator:  ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
 Join two list of integers in Python using + operator:  ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Program to join two list using (*) multiply operator

Consider an example to join two lists of in Python using * operator.

Mypro2.py

Output

Display integer List3  [1, 2, 3, 4, 5]
 Display integer List4  [6, 7, 8, 9, 10]
 Join two characters list in Python using * operator: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
 Join two integers list in Python using * operator: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Program to join two lists in Python using extend() method

Let's write a simple program to join two lists using extend() method in Python.

Prog.py

Output

Display the List1  [5, 10, 5]
 Display the List1  [2, 4, 6, 8]
 Display the List3  ['RED', 'BLUE', 'BLACK']
 Display the List4  ['BROWN', 'PURPLE', 'GREY']

 Adding two lists of integers in Python using the extend() function:  [5, 10, 5, 2, 4, 6, 8]

 Adding two lists of strings in Python using the extend() function:  ['RED', 'BLUE', 'BLACK', 'BROWN', 'PURPLE', 'GREY']





Latest Courses