Python lists


Lists is a sequence and a basic data structure.   A list may contain strings (text) and numbers.  

Video: Lists Get Exercises Quiz Get PDF

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


33 thoughts on “Python lists

  1. Ayesha - January 16, 2016

    How can i print the items in the list randomly

    1. Frank - January 24, 2016

      You can use the random.shuffle() method:

      #!/usr/bin/python
      import random
       
      list = [ "Drake", "Derp", "Derek", "Dominique" ]
      random.shuffle(list)
      print(list)
  2. José Chumán - December 8, 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

    1. Frank - December 8, 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.

  3. Mike - September 1, 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>?

    1. Frank - September 2, 2015

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

    2. Johnkw - September 10, 2015

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

    3. Lei - November 24, 2015

      pyhton 2.7.10 is ok

  4. Mike - September 1, 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

    1. Frank - September 1, 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
      1. Mike - September 1, 2015

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

        1. Frank - September 1, 2015

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

  5. Ben - August 27, 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?

    1. Frank - August 27, 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)