Linux, OpenSource, Programming And Hacks

Apr 8, 2017

How to Add / Append items to a list in Python

Apr 8, 2017
To add items to a list in python is a very general task that you might come across.

Here we have an empty list that we are populating with 10 items using the append method.

n = 10

list = []

for i in range(n):

print list

That's it!

Common Mistake

its a common mistake of doing something like this


for i in range(n):

this will throw an error IndexError: list assignment index out of range

Because the list is empty and list[i] is trying to access index which does not exist!

No comments:

Post a Comment