How do you combine all elements in a list python?

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']
    


    I have a list of string like:

    s = [["abc","bcd","cde"],["123","3r4","32f"]]
    

    Now I want to convert this to the following:

    output = ["abcbcdcde","1233r432f"]
    

    What is the pythonic way to do this? THanks

    asked Mar 29, 2012 at 4:24

    0

    >>> [''.join[x] for x in s]
    ['abcbcdcde', '1233r432f']
    

    answered Mar 29, 2012 at 4:25

    1

    >>> map[''.join, s]
    ['abcbcdcde', '1233r432f']
    

    That should do it

    answered Mar 29, 2012 at 4:29

    jamylakjamylak

    123k29 gold badges227 silver badges227 bronze badges

    2

    output = []
    for grp in s:
        output.append[''.join[grp]]
    

    answered Mar 29, 2012 at 4:27

    Jonathon ReinhartJonathon Reinhart

    127k32 gold badges245 silver badges316 bronze badges

    How about this:

    >>> map[lambda x: ''.join[x], s]
    ['abcbcdcde', '1233r432f']
    

    answered Mar 29, 2012 at 4:29

    Wil CooleyWil Cooley

    8906 silver badges18 bronze badges

    1

    Not a real answer, just want to check, what about reduce and operator.add, i read that in such a way both of them would perform quite fast and effective, or i am wrong?

    s = [["abc","bcd","cde"],["123","3r4","32f"]]
    
    from operator import add
    
    [reduce[add, x] for x in s]
    

    answered Mar 29, 2012 at 4:36

    Artsiom RudzenkaArtsiom Rudzenka

    26.9k4 gold badges32 silver badges51 bronze badges

    1

    Not the answer you're looking for? Browse other questions tagged python or ask your own question.

    How do you concatenate all elements in a list?

    To concatenate a list of integers an empty list is created as newlist = []. The extend method[] adds all the elements of the list till the end. To concatenate the list on integers “+” is used. The print[newlist] is used to get the output.

    How do you concatenate a list in python?

    1. Concatenation operator [+] for List Concatenation. The '+' operator can be used to concatenate two lists. It appends one list at the end of the other list and results in a new list as output.

    Can you merge a list in python?

    One simple and popular way to merge[join] two lists in Python is using the in-built append[] method of python. The append[] method in python adds a single item to the existing list. It doesn't return a new list of items, instead, it modifies the original list by adding the item to the end of the list.

    How do you add all items in a list in python?

    sum[] function in Python Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum[] which sums up the numbers in the list. Syntax: sum[iterable, start] iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.

    Chủ Đề