How to build a list using a loop python

后端 未结 2 652
长发绾君心
长发绾君心 2020-12-22 13:52

How do I get items in a python loop to build a list. In php I would use something like this:

$ar1 = array(\"Bobs\",\"Sams\",\"Jacks\"); 
foreach ($ar1 as $ar         


        
相关标签:
2条回答
  • 2020-12-22 14:22

    This should get you started:

    list1 = ['Me','You','Sam']
    list2 = ['Joe','Jen']
    
    for item in list2:
       list1.append(item)
    

    list1 now is ['Me', 'You','Sam','Joe','Jen']

    If you want a third list, simply define it and append to it instead.

    0 讨论(0)
  • 2020-12-22 14:27

    So a couple of things are wrong with your code.

    First:

    list = ("Bobs", "Sams", "Jacks"); should be list = ["Bobs", "Sams", "Jacks"]

    Second:

    foreach list2 in list1:
        list3 = list2 + " list item"
    

    should be

    list3 = []
    for item in list1:
       list3.append(item)
    
    0 讨论(0)
提交回复
热议问题