We don’t have arrays; we have Python lists. Lists are super dynamic because they allow you to store more than one “variable” inside of them. Lists have methods that allow you to manipulate the values inside them. There is actually quite a bit to show you here so let’s get to it.

sampleList = [1,2,3,4,5,6,7,8]
print (sampleList[1])


The brackets are just an indication for the index number. Like most programming languages, Python’s index starting from 0. So, in this example 1 is the second number in the list. Of course, this is a list of numbers, but you could also do a list of strings, or even mix and match if you really wanted to (not the best idea though). Alright, now let’s see if we can print out the whole list.

sampleList = [1,2,3,4,5,6,7,8]
for a in sampleList:
    print (a)


I told you we would come back to see how awesome the for loop is. Basically, variable a is the actual element in the list. We are incrementing an implicit index. Don’t get too worried about it. Just remember we are cycling through the list.

Common List Methods

There are number of methods for lists, but we will at least cover how to add and delete items from them. All of the list methods can be found on Python’s documentation website. Methods follow the list name. In the statement listName.append(2), append() is the method.

  • .append(value) – appends element to end of the list
  • .count(‘x’) – counts the number of occurrences of ‘x’ in the list
  • .index(‘x’) – returns the index of ‘x’ in the list
  • .insert(‘y’,’x’) – inserts ‘x’ at location ‘y’
  • .pop() – returns last element then removes it from the list
  • .remove(‘x’) – finds and removes first ‘x’ from list
  • .reverse() – reverses the elements in the list
  • .sort() – sorts the list alphabetically in ascending order, or numerical in ascending order

Try playing around with a few of the methods to get a feel for lists. They are fairly straightforward, but they are very crucial to understanding how to harness the power of Python.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Comment

  1. I like these tutorials. They are very good for someone with advanced knowledge of programming and it’s still possible to learn if you’re a beginner. I do have past experience with Python but I have defiantly learned a lot here. If one of the developers of this site or the person that wrote this tutorial are reading: Great job!

  2. My code:

    list=[‘8′,’@’,’l’,’l’,’e’,’h’,’ ‘,’e’,’h’,’t’,’ ‘,’t’,’a’,’h’,’w’]

    The expected output(the output in python idle editor) is
    [‘w’, ‘h’, ‘a’, ‘t’, ‘ ‘, ‘t’, ‘h’, ‘e’, ‘ ‘, ‘h’, ‘e’, ‘l’, ‘l’, ‘@’, ‘8’]
    [‘ ‘, ‘ ‘, ‘8’, ‘@’, ‘a’, ‘e’, ‘e’, ‘h’, ‘h’, ‘h’, ‘l’, ‘l’, ‘t’, ‘t’, ‘w’]

    The output in the current ‘Test Your Code’ is
    [‘w’, ‘h’, ‘a’, ‘t’, ‘ ‘, ‘t’, ‘h’, ‘e’, ‘ ‘, ‘h’, ‘e’, ‘l’, ‘l’, ‘@’, ‘8’]
    [‘a’, ‘ ‘, ‘h’, ‘h’, ‘t’, ‘t’, ‘ ‘, ‘e’, ‘e’, ‘h’, ‘l’, ‘8’, ‘@’, ‘l’, ‘w’]
    Please make a note that,
    the output of
    list=[‘8′,’@’,’l’,’l’,’e’,’h’,’ ‘,’e’,’h’,’t’,’ ‘,’t’,’a’,’h’,’w’]
    print(list) is
    [‘ ‘, ‘ ‘, ‘8’, ‘@’, ‘a’, ‘e’, ‘e’, ‘h’, ‘h’, ‘h’, ‘l’, ‘l’, ‘t’, ‘t’, ‘w’]

  3. Hello!
    I have the same problem with the "test your code"

    The code is:


    The result is: [2, 4, 3, 5, 7, 8]
    Shouldn’t be: [2, 3, 4, 5, 7, 8]?

    Could someone please help us?

    Thank you!

  4. Regarding the previous post, I tested the same sequence in IDLE and it returns the correct value.
    Furthermore, I have tested a different list here and it returns the correct value.

    So there must be a glitch somewhere. An interesting one 🙂

  5. Hello,
    I’ve trouble with this:

    lista_num = [1,2,3,4,5,1]

    gave me result:
    [1, 2, 3, 4, 5, 1]
    [1, 2, 3, 4, 5, 1]

    Should those function return the count and the index of the two number onto parenthesis ?

    Thanks for helping

    PS: this site is AWESOME !!!

  6. There seems to be a problem with the sort(). It sorts after calling it several times. Works fine when run through the Python IDLE.
    sampleList = [9,8,7,6,5,4,3,2,1]
    for a in sampleList:
    print (a)

  7. Important to know that Python lists is super dynamic is more useful than the array instruction in other programming languages.

  8. This is excellent! The way of explaining things are much easier and very helpful to understand for people like me.

  9. Hmm… Not sure I get the use of ‘Common List Methods’ as there are no examples given first to use something like:


  10. is my code wrong?

    newList = [1,2,3,4,5,5,6,7,8,9,9]
    newList.index(‘4’) #is there a mistake in this line?

    ValueError: list.index(x): x not in list #shouldn’t the line show 3 ?


  11. for a in range (0,(len(sampleList)-1)):
    if sampleList[a] == 2:

    what’s wrong with my Code ?

  12. Is there no way to remove an item from its Index( position), not the value itself, but its location in the list?

  13. ”’ must use single quotes to get characters ”’
    sample_List = [‘c’,’w’,’g’,’e’,’t’]

    ”’ should sort list first before trying to print ”’

    ”’ need to print each list element and not the whole list alltogether ”’
    for a in range (0,(len(sample_List))):
    print(sample_List [a])

  14. btw, this is by far the easiest to learn from even for a programmer.

    Love your site. I will retake this tutorial at least once a week for the first three months until I become familiar with python.

    i may have more in depth questions once i program my first ‘real’ application with Python

  15. listVar = “What a piece of work is a man.”

    This doesn’t work. Nor does a similar code work for strings.

    Anybody know why?


  16. sampleList = [1,2,3]
    print (sampleList)



    Why does the above result print inside of brackets instead of printing like this? 1,2,3,4

  17. markieej:


    means you are printing a variable of type List! Lists are always displayed in square brackets.

    You are not printing the elements of the list individually, but the list itself. I hope this helps

  18. markieej:


    means you are printing a variable of type List! Lists are always displayed in square brackets.

    You are not printing the elements of the list individually, but the list itself. I hope this helps

  19. another way to append at probably easier to understand (sorry NAM!) is:
    #now to set it its 0 1 2 not 123 remember like coordinates
    if we now print(list)
    it would be [0,2,3]

  20. An explanation of when to use different kinds of brackets would be thorough… ( [] introduced in last tutorial without explanation)

  21. It could be nice also to give an example with one of the suggested methods. I was really struggling to make .sort() to work. It was returning me errors or just “None” value. Finally I made it.
    list = [5,8,99,65,105,235,44]
    a = list.sort()
    for a in list:
    print (a)
    I also found out, that if I will put “a=list.sort()” in a loop after “print” statement – result will be the same. Donno why.

  22. Just a comment ¬¬¬
    I am new to Phyton, I do handle about 9 languages, I see no reason to call List a very well known and popular form of an indexed array.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.