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.
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.
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.
What is the function that counts number of elements in the list?
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.
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.
An explanation of when to use different kinds of brackets would be thorough… ( [] introduced in last tutorial without explanation)
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]
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:
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
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
This can also be used for print loops(The hard way)
lists = []
for x in range(5):
lists.append(“O”*5)
x=” “.join(lists[x])
print(x)
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
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
”’ 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])
Is there no way to remove an item from its Index( position), not the value itself, but its location in the list?
for a in range (0,(len(sampleList)-1)):
if sampleList[a] == 2:
sampleList[a].insert(‘0′,’2’)
what’s wrong with my Code ?
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
Hmm… Not sure I get the use of ‘Common List Methods’ as there are no examples given first to use something like:
.count(‘x’)
This is excellent! The way of explaining things are much easier and very helpful to understand for people like me.
I like that we went back to loops
https://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 "reverse" 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
Important to know that Python lists is super dynamic is more useful than the array instruction in other programming languages.
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)
Why I am not able to change my Image after getting to level 5 🙁
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 !!!
a= [a,s,d,f,g,w,e,v,x,d,w,e,z,g]
print(a.sort())
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 🙂
Hello!
I have the same problem with the "test your code"
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!
it works like this –
#this is one way
print(sorted(list))
# this is another way
list_sorted = sorted(list)
print(list_sorted)
I have tried the same code of program, however, i got the result as [2, 3, 4, 5, 7, 8], I wud suggest you try once again.
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’]
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!