Help guys, do that. How to merge two lists into one two-dimensional one in order to get the following picture:

Massiv1 = ['Stroka 1', 'Stroka 2', ...дальше много строк...] Massiv2 = [100, 200, ......] 

--- some code where Massiv1 and Massiv2 are glued together ---

Result:

 Massiv_sum = [['Stroka 1', 100],['Stroka 2', 200],.... и так далее ....] 

    2 answers 2

    To solve the problem, you must use the zip function:

     Massiv1 = ['Stroka 1', 'Stroka 2'] Massiv2 = [100, 200] Massiv_sum = list(zip(Massiv1, Massiv2)) Massiv_sum_result = [] for m_item in Massiv_sum: Massiv_sum_result.append(list(m_item)) 

    zip here is the zipper — sews the two halves:

    zipper

    • Your result is different from the required - you need a list of lists, and you have a list of tuples at the exit. - Enikeyschik
    • @ Enikeyshchik Updated example. But below they have already proposed a more elegant code - Fomina Ekaterina
    • @jfs The picture fits perfectly, thanks - Fomina Ekaterina

    This is where the zip() function comes in handy. But it returns a tuple of list items, so the list function needs to turn each resulting tuple into a list:

     Massiv_sum = [list(a) for a in zip(Massiv1, Massiv2)] 

    Result:

     [['Stroka 1', 100], ['Stroka 2', 200]]