How do you join two values in a list python?

You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.

>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> lst.extend(range(11, 14))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

So you can use list.append() to append a single value, and list.extend() to append multiple values.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, we require to merge some of the elements as single element in the list. This is usually with the cases with character to string conversion. This type of task is usually required in the development domain to merge the names into one element. Let’s discuss certain ways in which this can be performed.

    Method #1 : Using join() + List Slicing
    The join function can be coupled with list slicing which can perform the task of joining each character in a range picked by the list slicing functionality.

    test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']

    print ("The original list is : " + str(test_list))

    test_list[5 : 8] = [''.join(test_list[5 : 8])]

    print ("The list after merging elements : " +  str(test_list))

    Output:

    The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
    The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
    

    Method #2 : Using reduce() + lambda + list slicing
    The task of joining each element in a range is performed by reduce function and lambda. reduce function performs the task for each element in the range which is defined by the lambda function. It works with Python2 only

    test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']

    print ("The original list is : " + str(test_list))

    test_list[5 : 8] = [reduce(lambda i, j: i + j, test_list[5 : 8])]

    print ("The list after merging elements : " +  str(test_list))

    Output:

    The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
    The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
    


    Let’s see how to concatenate two lists using different methods in Python. This operation is useful when we have numbers of lists of elements which needs to be processed in a similar manner.

    Method #1 : Using Naive Method

    In this method, we traverse the second list and keep appending elements in the first list, so that first list would have all the elements in both lists and hence would perform the append.

    test_list1 = [1, 4, 5, 6, 5]

    test_list2 = [3, 5, 7, 2, 5]

    for i in test_list2 :

        test_list1.append(i)

    print ("Concatenated list using naive method : " 

                                  + str(test_list1))

    Output:

    Concatenated list using naive method : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    

     
    Method #2 : Using + operator

    The most conventional method to perform the list concatenation, the use of “+” operator can easily add the whole of one list behind the other list and hence perform the concatenation.

    test_list3 = [1, 4, 5, 6, 5]

    test_list4 = [3, 5, 7, 2, 5]

    test_list3 = test_list3 + test_list4

    print ("Concatenated list using + : "

                       + str(test_list3))

    Output:

    Concatenated list using + : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    

    Method #3 : Using list comprehension

    List comprehension can also accomplish this task of list concatenation. In this case, a new list is created, but this method is a one liner alternative to the loop method discussed above.

    test_list1 = [1, 4, 5, 6, 5]

    test_list2 = [3, 5, 7, 2, 5]

    res_list = [y for x in [test_list1, test_list2] for y in x]

    print ("Concatenated list using list comprehension: "

                                         + str(res_list))

    Output:

    Concatenated list using list comprehension: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    

     
    Method #4 : Using extend()

    extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the inplace extension of first list.

    test_list3 = [1, 4, 5, 6, 5]

    test_list4 = [3, 5, 7, 2, 5]

    test_list3.extend(test_list4)

    print ("Concatenated list using list.extend() : "

                                   + str(test_list3))

    Output:

    Concatenated list using list.extend() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    

     
    Method #5 : Using * operator

    Using * operator, this method is the new addition to list concatenation and works only in Python 3.6+. Any no. of lists can be concatenated and returned in a new list using this operator.

    test_list1 = [1, 4, 5, 6, 5]

    test_list2 = [3, 5, 7, 2, 5]

    res_list = [*test_list1, *test_list2]

    print ("Concatenated list using * operator : " 

                                  + str(res_list))

    Output:

    Concatenated list using * operator : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    

     
    Method #6 : Using itertools.chain()

    itertools.chain() returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when concatenated list has to be used just once.

    import itertools

    test_list1 = [1, 4, 5, 6, 5]

    test_list2 = [3, 5, 7, 2, 5]

    res_list = list(itertools.chain(test_list1, test_list2))

    print ("Concatenated list using itertools.chain() : "

                                          + str(res_list))

    Output:

    Concatenated list using itertools.chain() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
    


    How do you join two items in a list python?

    The most conventional method to concatenate lists in python is by using the concatenation operator(+). The “+” operator can easily join the whole list behind another list and provide you with the new list as the final output as shown in the below example.

    How do you combine elements in a list?

    This operation is useful when we have numbers of lists of elements which needs to be processed in a similar manner..
    Method #1 : Using Naive Method..
    Method #2 : Using + operator..
    Method #3 : Using list comprehension..
    Method #4 : Using extend().
    Method #5 : Using * operator..
    Method #6 : Using itertools.chain().

    How do you join a specific part of a list in python?

    To join specific list elements in Python:.
    Use list slicing to select the specific elements in the list..
    Use the str. join() method to join the elements into a string..
    Replace the list elements with the string..