You (Level 0)
0% Complete
None

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.

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

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

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.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.

Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

If you enjoyed this resource, support me by sharing this page with others.

## Comment on

• #### Ark Nov. 15, 2016, 12:46 a.m.

What is the function that counts number of elements in the list?

• #### Foro5 March 28, 2015, 6:42 a.m.

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.

• #### Aksinia March 24, 2015, 2:18 p.m.

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.

• #### yulan March 11, 2015, 7:18 p.m.

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

• #### Guru the Wizard Feb. 13, 2015, 5:58 p.m.

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

• #### NAM Jan. 7, 2015, 8:07 a.m.

markieej: print(sampleList) 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

• #### NAM Jan. 7, 2015, 8:07 a.m.

markieej: print(sampleList) 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

• #### markieej Dec. 30, 2014, 8:40 p.m.

sampleList = [1,2,3] sampleList.append(4) print (sampleList) Result: [1,2,3,4] Why does the above result print inside of brackets instead of printing like this? 1,2,3,4

• #### Neo Dec. 8, 2014, 1:27 p.m.

This can also be used for print loops(The hard way)

• #### guest6 Dec. 1, 2014, 6:05 p.m.

lists = [] for x in range(5): lists.append("O"*5) x=" ".join(lists[x]) print(x)

• #### archangel Nov. 27, 2014, 7:56 p.m.

listVar = "What a piece of work is a man." listVar.count('a') This doesn't work. Nor does a similar code work for strings. Anybody know why? Thanks

• #### vingtsun01 Nov. 4, 2014, 7:05 p.m.

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

• #### vingtsun01 Nov. 4, 2014, 7:04 p.m.

''' must use single quotes to get characters ''' sample_List = ['c','w','g','e','t'] ''' should sort list first before trying to print ''' sample_List.sort() ''' need to print each list element and not the whole list alltogether ''' for a in range (0,(len(sample_List))): print(sample_List [a])

• #### NaveenJuly 5, 2014, 2:55 p.m.

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

• #### ernd59June 27, 2014, 1:31 a.m.

for a in range (0,(len(sampleList)-1)): if sampleList[a] == 2: sampleList[a].insert('0','2') what's wrong with my Code ?

• #### AnoubysJune 10, 2014, 10:11 a.m.

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

• #### Malthus101May 31, 2014, 11:43 a.m.

Hmm... Not sure I get the use of 'Common List Methods' as there are no examples given first to use something like: .count('x')

• #### iPhoenixApril 24, 2014, 1:59 p.m.

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

• #### hawk23March 8, 2014, 10:39 a.m.

I like that we went back to loops

• #### SatyaMarch 7, 2014, 6:45 p.m.

http://www.afterhoursprogramming.com/tutorial/Python/Lists/ When I tried to append an element to an existing list, the python emulator went in to infinite loop. The command &quot;reverse&quot; gave me values 'none' instead of giving me a reversed list.Please check. myList = [1,2,3] for a in myList: print (a) print myList.reverse() OutPut: 1 None 2 None 3 None

• #### Lotfi GHAZOUANIFeb. 26, 2014, 3:42 p.m.

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

• #### kgoveasJan. 20, 2014, 7:03 a.m.

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] sampleList.sort() sampleList.sort() sampleList.sort() sampleList.sort() sampleList.sort() for a in sampleList: print (a)

• #### AqdasJan. 6, 2014, 3:57 a.m.

Why I am not able to change my Image after getting to level 5 :(

• #### The AztekDec. 9, 2013, 4:22 a.m.

Hello, I've trouble with this: lista_num = [1,2,3,4,5,1] lista_num.count(1) print(lista_num) lista_num.index(2) print(lista_num) 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 !!!

• #### R-DubDec. 8, 2013, 11:56 p.m.

a= [a,s,d,f,g,w,e,v,x,d,w,e,z,g] print(a.sort())

• #### RedAlexNov. 21, 2013, 6:10 a.m.

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 :)

• #### RedAlexNov. 21, 2013, 6:04 a.m.

Hello! I have the same problem with the &quot;test your code&quot; The code is: list=[5,2,4,7,3,8] list.sort() print(list) 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!

• #### Raviteja PanamgipalliJuly 16, 2013, 2:41 a.m.

My code: list=['8','@','l','l','e','h',' ','e','h','t',' ','t','a','h','w'] list.reverse() print(list) list.sort() print(list) 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'] list.sort() print(list) is [' ', ' ', '8', '@', 'a', 'e', 'e', 'h', 'h', 'h', 'l', 'l', 't', 't', 'w']

• #### EamonnJune 7, 2013, 7:46 a.m.

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!