Python Lists
Hello guys, here we will talk about python list. List is the most useful
and frequent used data type in python.
Creating
list
It is very easy to create a
list in python, anything that comes between these brackets, [ ].
It can be pure integer or pure alpha or mixed.
We will now look into a
program,
Let’s have a look
into the output too,
Similar to nested
loops (check out loops, here), we have nested list:-
Accessing and
indexing of list
Accessing elements of list in python isn’t that much
of a difficulty.
Look at the code below,
Output will be not shocking for some and killing it for ;)
Change, add
and deletion of elements in list
List is mutable, i.e. we can
easily make a change or add or delete members of list.
Trying to go out of the index
will bring up IndexError. We use only
integer while indexing other than an integer will give TypeError.
Changing of elements,
Output,
Adding of elements in the list
can be done using two in-built functions, known as append() and extend()
If we have to add one item we
will go for append() else if more
than one we go for extend(). We
should know that this will make the adding element go last in the list.
Predict the output.
We can actually add an element
to a desired location by insert(),
Output
Deleting
or removing elements of a list
We can easily delete elements
of the list or the whole list with del.
Check out the example,
Output is as given,
Well as soon as we deleted the
list the compiler no longer can detect the past initialized list.
We also have pop() and
remove() to remove items from the list.
The clear() method will empty
the list.
Output
We can also remove a range of
elements from the list by assigning them to an empty list.
Output
Python
list method
Above we have already
discussed few of the methods such as append(), extend(), insert(),
remove(),pop(),clear().
We will talk about the rest
now.
Index()
It returns the index value of
the element present in the list.
The syntax is,
list.index(element,start,end)
|
Note:- The
index() method only returns the first occurrence of the matching element.
Output
count()
count() returns the occurrence of the element.
The syntax is,
Check out the output on your own.
sort()
The sort() method sorts the elements of a list in a
specific ascending or descending order.
The syntax is,
list.sort(key=.....,
reverse=......)
|
There is an alternative python’s built-in sorted()
function for the same.
Note: The simplest difference
between sort() and sorted() is: sort() changes the list directly
and doesn't return any value, while sorted() doesn't
change the list and returns the sorted list.
Parameter of sort()
a) Reverse: if True, then the sorted list is
reversed.(actually descending order)
b) Key: function
that serves as a key for the sort comparison
Output
If you want your
own implementation for sorting, the sort() method also accepts
a key function as an optional parameter.
We will discuss
this later as we haven’t yet discussed functions in python.
reverse()
This reverse()
method reverses the elements of a list.
The syntax is,
The reverse()
doesn’t take any argument.
The reverse()
method doesn’t return any value. It updates the existing list.
Output
Another way for reversing a list is by using slicing
operator, :
Output
If you need to
access individual elements of a list in the reverse order, it's better to use reversed() function.
Output
Similar concept is also used for copy()
Copying a list through slicing operator
Output
List comprehension is a must topic in list, it makes the code easy and one liner and simple, what not. To learn, click here
Hope all your doubts regarding this are clear now.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!