python: how to append multiple lists to one

时间:2018-12-03 13:13:11

标签: python

I have a list that should contain all my other lists. Currently I append every list separately but that just looks pretty ugly..

looplist = []  # initiate an empty list that contains all date from the other lists 
[...]
looplist.append(internallist1)
[...]
looplist.append(internallist10)

the internallists are all getting initialized and filled in a for loop

4 个答案:

答案 0 :(得分:2)

You can simply use + to merge them.

You may check for more info.

If you want to have list of lists, check this topic.

答案 1 :(得分:1)

listOne.extend(anotherList)

this could help you: https://docs.python.org/3/tutorial/datastructures.html

you can also do listOne+=anotherList and this is less expensive, as it doesn`t involve a function call like extend

答案 2 :(得分:1)

要回答您的问题,只需使用10个列表初始化循环列表即可。

looplist = [internallist1,
            internallist2,
            internallist3] #Note: internallist3,] is also valid, python allows trailing comma. Nifty!

但是,如果这是您的实际用例,那么实际上不应首先将10个列表单独命名为列表。只需使用looplist[0] through looplist[9]即可。

答案 3 :(得分:0)

zip方法可能对您有用,因为您声明输出应该:

  

看起来像[[list1],[list2],...,[list n]]

在您的情况下,代码类似于

looplist = list(zip(internallist1,[...], internallist10))