List creation and manipulation In Python
All programming language environments provide for data
creation, storage and retrieval methods, that enables the
them to perform data proccessing activities. In this
article we will see what a
python list is and how powerful is it in data proccessing, storage
and and retrieval. we will also see some examples of syntax codes used
to perform these data proccessing in python.
Well, list essentially is used to store information. List stores not
only related information, a buch of different data values can be
stored in the list and organise for later retrieval, just like array in Java,
Javascript, PHP and other programming languages; data are stored
in a list and keep tract and access the list values anytime for the
purpose of updating, manipulating and correcting any eroneous values
or list member in the list. So we can think of a list as a vault where
treasures are kept for later use.
Values are stored in a list with each having and index number.
It is through this index that any list value
could be accessed and called for further data proccessing.
Without the index, accessing list values would not be possible.
Just like our house address in our community,
in the same manner, an index number in a list serves as the address
of a value in that list.
List is dynamic in nature, meaning that its contents can be changed -
you can add to, and remove values from it, so
unlike python turple, list is mutable.
How to create a list?
a list is created by giving the list a name and its associated values
are surrended by a square bracket. Syntax for creating a list is :
list name = [values]
In the box below, let us create a book list and do some data proccessing
on it.
books = ["Biology", "Physics", "Chemistry", "Geograpy"]
Explanations:
We have created a variable called 'books'
and put its values in a list. This list contains four different
book elements which is refered to as list values - biology, physics,
Chemistry, and geography books. At this stage, we can do a lot of things
with this list, we can modify this list in many ways. We can add, delete, and update the list members;
we can also delete the entire list.
What next?:
We have created a book list above, the next is to access the list and
perform operation on its values.
To see the entire list values, we will simply say:
print(books)
['Biology','Physics','Chemistry','Geography']
>>>
But some times it is a good idea to see the content of the list individually i.e
list values outside the list itself, in that case we need to loop through
the list and display its contents like this:
for book in books:
print(book)
Biology
Physics
Chemistry
Geography
>>>
Accessing Individual list member
To access any list values in python, the index number of such list value
is referenced. Values indexing in python start from 0 to the last number in
in the list. In our list of books above, four books are declared, that is,
we have four books in total. But when python is indexing, index numbering
starts with zero (0), so in our own book list we have 0 - 3 indexing number
of which the book in the first position takes index [0] Biology,
index [1] physics, index [2] Chemistry, and of course
Geography occupies the last index number in the list, which is index [3].
Let us try and access Chemistry, Chemistry is the third value in the
list, hence it occupies index of [2]. the command will be:
print (books[2])
Chemistry
>>>
Updating a list
Let us assume we are operating a library. As a librarian, you periodically
acquire new books and also weed off outdated volume of books. In this scenario
our book list represents a library that has four books, we just purchased
a copy of economics book title into the library, how do we add this book on the
library shelf? And when certain books are weeded (remove) from the library,
how do we remove the book? These are explained in the following paragraphs.
Adding value to a list
The syntax below does it. Python provides several methods
to add new element to the existing list. You can use append() and
insert() methods to add new element to an existing list. Now use
append() method sytax below to add our newly purchased economic book to
our list
Adding values by append() method
You can add a value to your list by calling your list name and append() method with dot(.)
separating the list name and the append method like below.
books.append('Economics')
['Biology','Physics','Chemistry','Geography', 'Economics']
>>>
We have just seen the adding to the list by append() method and the resultant output given,
let us add value into this list by insert() method.
Adding values using insert() method