In Python, you can use the list.append method to add items to the end of a list. If you want to specify where an item should appear in the list, you’ll want to use the list.insert method instead. This wikiHow article will show you two different ways to add items to lists in Python loops.
StepsMethod 1Method 1 of 2:Appending to the End of a List
1Use list.append(item) to add items to the end of a list. You’ll also use list.append when you want to populate an empty list with specific items. Replace list with the name of your list, and item with the item you want to add.X In this example, we want to add the numbers 0 through 9 to an empty list. We’ll use a for loop to do so: numberList = for i in range(10): numberList.append(i)print(‘List of all numbers:’, numberList)The output would be List of all numbers: Because we defined i as range(10), using numberList.append(i) added the numbers 0 through 9 to our list. Method 2Method 2 of 2:Adding to Specific Position in a List
1Use list.insert(position, item) to insert an item at a specific location. You’ll replace list with the name of your list, position with the position in the list into which you want to insert the new item, and item with the item you’re adding.X In this example, we’ll use a for loop to insert the word orange into our list of colors. We want our list to be in alphabetical order, so we’ll place orange at position 3 (between green and pink): colorList = for i in colorList: print(i) colorList.insert(3, orange) print(‘All colors:’)for l in colorList: print(l)We’d see two things in the output: the first would be a list of the colors in the list (without orange added), and the second would be a list of all colors including orange at the position we specified.