Home » Tutorials » Python » Lists


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])
Result 2

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)
Result 1

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.

Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Lists". After Hours Programming. Accessed on July 4, 2024. https://www.afterhoursprogramming.com/tutorial/python/lists/.

  • Stewart, Suzy. "Lists". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/lists/. Accessed 4 July, 2024.

  • Stewart, Suzy. Lists. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/lists/.

31 thoughts on “Lists”

  1. 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.

  2. 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]

  3. 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

  4. 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

  5. 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

  6. ”’ 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])

  7. 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 ?


  8. 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)

  9. 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 !!!

  10. 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 🙂

  11. 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!

  12. 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’]

  13. 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!

Leave a Comment

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