python logo

Python lists

Python hosting: Host, run, and code Python in the cloud!

Lists is a sequence and a basic data structure.   A list may contain strings (text) and numbers.  A list is similar to an array in other programming languages, but has additional functionality.

Related Course:
Python Programming Bootcamp: Go from zero to hero

Python List


We define lists with brackets []. To access the data, these same brackets are used.
Example list usage:

#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
print(l[0]) # print first element
print(l[1]) # prints second element

Add/remove


We can use the functions append() and remove() to manipulate the list.

#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
l.append("Victoria") # add element.
print(l) # print all elements
l.remove("Derp") # remove element.
l.remove("Drake") # remove element.
print(l) # print all elements.

Sort list


We can sort the list using the sort() function.

#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
l.sort() # sorts the list in alphabetical order
print(l) # prints all elements

If you want to have the list in descending order, simply use the reverse() function.

#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
l.sort() # sorts the list in alphabetical order
l.reverse() # reverse order.
print(l) # prints all elements

If you are new to Python programming, I highly recommend this book.

Download Python Exercises

BackNext





Leave a Reply:




Hattem Mon, 08 Jun 2015

wow this is interest

Psk Sat, 18 Jul 2015

Can ya make a 2D and 3D array too?

Frank Sat, 18 Jul 2015

Yes, there is no limit to the number of dimensions in an array.

a 2d array:

#!/usr/bin/env python3

list = [[0, 1], [1, 2], [2, 3]]

print(list)
print(list[0][0])
print(list[1][1])

a 3d array:

#!/usr/bin/env python3

list = []

for i in xrange(3):
list.append([])
for j in xrange(3):
list[i].append(i+j)

print(list)
print(list[0][0])
print(list[1][2])
Bheesam Jangra Sat, 18 Jul 2015

when i am trying for this 3D array i am getting below error:

Traceback (most recent call last):
File "hello.py", line 3, in
for i in xrange(3):
NameError: name 'xrange' is not defined

i have python35-32 installed on my PC and when i am trying to run this i am getting above error.

Frank Sat, 18 Jul 2015

Try changing to:

#!/usr/bin/env python3

list = []

for i in range(3):
list.append([])
for j in range(3):
list[i].append(i+j)

print(list)
print(list[0][0])
print(list[1][2])

Also try:

#!/usr/bin/env python3

list = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

print(list)
print(list[0][0])
print(list[1][1])
Abhishek Tue, 11 Aug 2015

what is difference between list = [[0, 1], [1, 2], [2, 3]] and list = ([0, 1], [1, 2], [2, 3]) both appears to be array?

Staff Tue, 11 Aug 2015

Good question! The second is a tuple, not a list. A tuple is a collection of variables that cannot be changed.
Once you try to set one of the tuple variables, you will get an exception. Try with this code:

#!/usr/bin/env python

setA = [[0, 1], [1, 2], [2, 3]]
setB = ([0, 1], [1, 2], [2, 3])

print(setA)
print(setB)

setA[0] = [2,3]
setB[0] = [2,3]

print(setA)
print(setB)
Jonas Mon, 17 Aug 2015

When I type

l = [ "Drake", "Derp", "Derek", "Dominique" ]
and
print l


it gives an error:
SyntaxError: Missing parentheses in call to 'print'

Frank Mon, 17 Aug 2015

Hi, you are using Python 3.x. Use: print(l)

Ben Tue, 18 Aug 2015
a=['james','make','susan']
print a.reseve();

I expect the result is :['susan', 'make', 'james'] , but the it showed me like this :
>>>
None
james
>>>

Did anyone can tell me why

Frank Tue, 18 Aug 2015

Hi Ben, this will work:

>>> a=['james','make','susan']
>>> a.reverse()
>>> print a
['susan', 'make', 'james']

This is because reverse() returns None (see the function call paragraph). The function call changes its internal list but does not return the new list.
Some more examples:

>>> a=[1,2,3]
>>> print a.reverse()
None
>>> a
[3, 2, 1]

or

>>> a=['james','make','susan']
>>> print a.reverse()
None
>>> a
['susan', 'make', 'james']
Ben Tue, 18 Aug 2015

Thank you so much . this function different from PHP/PERL

Nagendra Fri, 21 Aug 2015

Hello ben,
a.reverse function will perform the reverse operation, it will store in the variable,
so call the variable then you got the answer ok
Regards,
Nagendra N

Blooper Sat, 22 Aug 2015

I have a similar problem.
I defined the variable first as n.
When I command "print (n.sort())" or "print (n.reverse())" it responds "None" each time.

Frank Sat, 22 Aug 2015

Call n.sort() or n.reverse() then print(n)

Ben Thu, 27 Aug 2015

Hi,

a=[]

for i in range(1,10):
if i%2 !=0:
a.append[i]
print a[0]


But it showed : File "C:\Python27\learn\test.py", line 48, in
a.append[i]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'?

why I can't via loops to append value to a list?

Frank Thu, 27 Aug 2015

Hi Ben, use brackets for append. Like this:

#!/usr/bin/env python

a=[]

for i in range(1,10):
if i%2 !=0:
a.append(i)

print(a[0])
print(a)
Mike Tue, 01 Sep 2015

hey having issues with this part

#!/usr/bin/python
l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
print(l[0]) # print first element
print(l[1]) # prints second element
Output:
on the print l[0]


im on python 3 i know nothing about this im just here trying to learn any help is apreciated

Frank Tue, 01 Sep 2015

Hi Mike,

In Python 3.x the brackets are required:

#!/usr/bin/python3

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l) # prints all elements
print(l[0]) # print first element
print(l[1]) # prints second element

Mike Tue, 01 Sep 2015


#!/usr/bin/python
l = [ "Drake", "Derp", "Derek", "Dominique" ]
print l # prints all elements
print l[0] # print first element
print l[1] # prints second element


Output:
if when i try print l[0] and it doesnt work is it because im using python 3>?

Mike Tue, 01 Sep 2015

thank you sir , if im a beginner and am going thru tutorials would you recommend i download the other version ?

Frank Tue, 01 Sep 2015

Hi Mike,
If you use brackets you can use Python 3.x.
All of them should work

Frank Wed, 02 Sep 2015

Yes. Use print(l[0]) for Python 3.x

Johnkw Thu, 10 Sep 2015

i dont think its because of the python version..im using python 3 and its working very well even vat that part

Tinh Doan Wed, 16 Sep 2015

when I run this code on "http://pythonspot.com/run.php" both of them is the same??
output is:

[[0, 1], [1, 2], [2, 3]]
([0, 1], [1, 2], [2, 3])

but when I run local, I get an error:
TypeError: 'tuple' object does not support item assignment

Tinh Doan Wed, 16 Sep 2015

Why don't use:

list = [[0, 1, 4], [1, 2, 5], [2, 3, 6]]        
print(list)
print(list[0][0])
print(list[1][2])

Frank Wed, 16 Sep 2015

The first one defines a list, the second one a tuple. Tuples cannot be given new values. You can read more on tuples here.

Frank Wed, 16 Sep 2015

That should work too, only difference is first one is dynamic second static. It depends on your application

Lei Tue, 24 Nov 2015

pyhton 2.7.10 is ok

José Chumán Tue, 08 Dec 2015

Is there any way to print like this without using any loop? I'm using Python 3.x


l = [ "Drake", "Derp", "Derek", "Dominique" ]
Output:
Drake
Derp
Derek
Dominique

Frank Tue, 08 Dec 2015

Loops are preferred but yes, you can print them one by one:

l = ["Drake","Derp","Derek","Dominique"]
print(l[0])
print(l[1])
print(l[2])
print(l[3])

There is another method, but loops would create the cleanest code.

Ayesha Sat, 16 Jan 2016

How can i print the items in the list randomly

Frank Sun, 24 Jan 2016

You can use the random.shuffle() method:

#!/usr/bin/python
import random

list = [ "Drake", "Derp", "Derek", "Dominique" ]
random.shuffle(list)
print(list)