Home » Python Lists : the basics

Python Lists : the basics

by maxguy71

The List is an extremely versatile datatype which is available in Python and can be written as a list of comma-separated values between square brackets.

Lets look at some important information on lists

A List is a collection which is ordered and changeable.
You can have duplicate members in a list.
Lists are written with square brackets.
The items (values) in the list are separated by commas
Items in a list need not be of the same type.

Lets look at some examples of lists

list1 = [‘mercury’, ‘venus’, ‘earth’, ‘mars’];
list2 = [1, 2, 3, 4, 5 ];
list3 = [“a”, “b”, “c”, “d” , “e”, “f”]
list4 = [1, 2, ‘three’, ‘four’]

lets try this out, create a file called lists.py and enter the following

list1 = ['mercury', 'venus', 'earth', 'mars'];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d" , "e", "f"]
list4 = [1, 2, 'three', 'four']
​
print(list1)
print(list2)
print(list3)
print(list4)

Run this from the command line with python lists.py and you should see something like this

lists example

Accessing items in the list

You can access an item in a list by referring to the index number, this starts at 0 and can also use negative indexing which means you begin at the end, -1 refers to the last item of the list

You can also specify a range of indexes by specifying where to start and where to end the range

Lets look at an example of these. We will return the first item, items 2,3 and 4 and the last item.

#!/usr/bin/python

planets = ['mercury', 'venus', 'mars', 'earth', 'jupiter','saturn', 'uranus', 'neptune'];

print ("planets[0]: ", planets[0])
print ("planets[2:5]: ", planets[2:5])
print ("planets[-1]: ", planets[-1])


Save this as list1.py and run it from the command line with python lists1.py. You should see something like this

 

You can also leave out the start or end values. By leaving out the start value, the range will start at the first item and if you leave out the end value, the range will go on to the end of the list

Lets see an example of this

#!/usr/bin/python

planets = ['mercury', 'venus', 'mars', 'earth', 'jupiter','saturn', 'uranus', 'neptune'];

print ("planets[2:]: ", planets[2:])
print ("planets[:5]: ", planets[:5])

Run it as before and you will something like this

Add Items To a list

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to the elements in a list using the append() method. Lets look at examples of this

#!/usr/bin/python

years = ['1985', '1988', '1999', '2005', '2009' ];
print (years)
print ("Index 1 = ")
print (years[1])
years[1] = 2001;
print ("Index 1 = ")
print (years[1])
print ("Append 2020 on the end = ")
years.append('2020')
print(years)

Again create a python file and run it, you should see this

Remove Items in a list

There are several ways to remove items in a list, we will look at them here

The remove() method will remove a specified item
The pop() method will removes a specified index, (or the last item if index is not specified)
The del keyword removes the specified index and can delete the whole list if an index is not set

Lets look at an example

#!/usr/bin/python

years = ['1985', '1988', '1999', '2005', '2009' ];
print (years)
#removes the '2005' item
years.remove('2005')
print (years)
#remove the first item
del years[0]
print (years)
#removes the last item
years.pop()
print (years)

Again save this and run this example and this is what you will see

list remove items

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More