Skip to content

Creating an empty list in Python  yttinyiconblue

Creating an empty list is easy, you just use the square backets [ ].

myList = []

Creating a list with items in it.

animals = ["dog","cat","mouse"]

Adding items to a list  yttinyiconblue

Adding to the end of the list

If you want to add an item to the end of a list, you need to use the append method.

animals = ["dog","cat","mouse"]
animals.append("chicken")

Adding to the beginning of the list

If you want add an item to beginning of a list(or at any given position), you need to use the insert method.

animals.insert(0,"bat")

Removing items from lists  yttinyiconblue

When removing items from a list you can use a number of different methods.

myList = ["Leonardo","Raphael","Donatello","Michaelangelo"]

The list.pop() method

This returns the first item from the list and deletes the item from the list. Useful if you have a list of items that you want to process and want to discard each item once you have finished with it.

item = mylist.pop()
 
print ("Goodbye", item)

 

the del list[i] function

This is similar to the pop() method but it just deletes the item.

del myList[0] #Delete the first item from the list

Looping through lists  yttinyiconblue

Looping through a list in Python is easy, we just use a for loop!

mixedList = ["dog", 4, 3.42,"cat"]
for item in mixedList:
    print(item)

Using if statements in lists  yttinyiconblue

Of course you can also use if statements while looping through your lists.

for temp in [-50,-30,10,40,50,100]:
    if temp > 80:
        print(temp, " Degrees. Wow that's hot!")
    elif temp > 20:
        print (temp, " Degrees. Mmmm nice and warm :-) ")
    else:
        print(temp, " Degrees. Freezing!!!!")

Create 2 Dimensional lists  yttinyiconblue

Sometimes we want to represent table data in python, and to do this we need to use a 2D list.

namesagestable

Sometimes  we want to represent tabled data in lists within Python. We can achieve this through the use of 2 dimensional lists(arrays) – lists within lists.

nameList = [["bob",20],["Sally",45],["Julie",30],["Fred",15]]
for name,age in nameList:
    print(name ,"is ", age, "years old.")

Zipping lists together and unzipping them

If you want to store the names and ages in two separate lists, then you can use the zip function to combine them!

names = ["bob","sally","fred","harry"]
ages = [20,40,35,200]
for name,age in zip(names,ages):
    print(name, age)

This method of combining separate lists is also much more readable!

Lists have lots of inbuilt methods that are very useful!

myList = ["Treacle","Custard","Chicken"]
 
myList.sort() # Sorts the list in place
 
myList.reverse() # Reverses the list
 
myList.pop() #Returns the first item from the list and removes it from the list
 
myList.count("Chicken") # Returns the number of times an item can be found in the list
 
"Dog" in myList # Can be used with if statements to see if an item is in a list
 
myList.index("Treacle") # Returns the position of the item in the list.

 

href=”http://openbookproject.net/thinkcs/python/english3e/lists.html”>http://openbookproject.net/thinkcs/python/english3e/lists.html