Skip main navigation

Lists in Data Structures

Data structures have many purposes. One of them is to allow you to run looping code. When working with data, you need to organise it well. That’s why Python and other programming languages offer you data structures as a way to group, store, and work with information. These structures come in a few forms.

Data structures have many purposes. One of them is to allow you to run looping code. When working with data, you need to organise it well. That’s why Python and other programming languages offer you data structures as a way to group, store, and work with information. These structures come in a few forms.

Data Lists in Python

You may wish to group related items or data for simplifying or ordering information, condensing your code or performing the same actions on several items at the same time. You can do this using lists. Lists are an ordered collection of data or items, which can be different types (although they usually aren’t). Lists are adaptable – they can contain duplicate members, be reordered, added to and edited; as such, they are described as being mutable.

In Python, we use square brackets ([ ]) when defining a list.

 

Try to create a list of fruit with this code example:

 

myFruitList = ['apple', 'banana', 'pear']
print(myFruitList)

 

Output:

 

[‘apple’, ‘banana’, ‘pear’]

 

You can access list items using the index. Index values start from zero.

 

This example uses the previous defined list. Try adding this code to the previous example:

 

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

 

Output:

 

apple
banana
pear

 

We can also use indices counted from the end of the list to retrieve list items by using negative numbers:

 

Code:

 

print(myFruitList[-1]) 

 

Output:

 

pear

 

In this example, you retrieved the first item counting from the end of the list.

 

Range of indices

 

You can also get items within a certain range:

 

Use this code example to print the first to fourth items from the list:

 

myFruitList = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
print(myFruitList[0:3])

 

Output:

 

['apple', 'banana', 'cherry']

 

You can do the same with negative indices:

 

Code:

 

myFruitList = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
print(myFruitList[2:-1])

 

Output:

 

['cherry', 'orange', 'kiwi', 'melon', 'mango']

 

In case you want to know how many items are in a list, you can use the len() function, which returns the list’s length. This is helpful if you want to edit an element close to the end of a list.

 

Try this code example to see how many items are in the list of fruit:

 

myFruitList = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
print(len(myFruitList))

 

Output:

 

8

 

You can also find out how many unique items are in a list using the set() function. The set object rejects duplicates and will therefore exclude them from the list. Try this code:

 

myFruitList = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
print(len(set(myFruitList)))

 

Output:

 

7

 

You can also add or remove items from the list. Try it:

 

myFruitList= ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
myFruitList.append('pear')
myFruitList.remove('apple')
print(myFruitList)

 

Output:

 

['banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana', 'pear']

 

Use .append to add an item and .remove to remove an item.

 

List items are also changeable. You can specify an index and assign a new value, as in this code example:

 

myFruitList= ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']
myFruitList[0] = 'strawberry'
print(myFruitList)

 

Output:

 

['strawberry', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'banana']

 

In the previous example, you specified an index and assigned it a new value.

 

Python also allows you to merge two or more lists. Try this example:

 

list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = list_one + list_two
print(list_three)

 

Output:

 

[1, 2, 3, 4, 5, 6]

 

This table summarises the list functions and their purposes:

 

 

Function Description
append() Adds an element at the end of the list.
clear() Removes all the elements from the list.
copy() Returns a copy of the list.
count() Returns the number of elements with the specified value.
extend() Adds the elements of a list (or any iterable) to the end of the current list.
index() Returns the index of the first element with the specified value.
insert() Adds an element at the specified position.
len() Returns the number of elements in a list.
pop() Removes the element at the specified position.
remove() Removes the item with the specified value.
reverse() Reverses the order of the list.
set() Returns a set of the given list.
sort() Sorts the list.

You now know how lists work!

This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education