Python List | python tutorial for beginners

Posted by

Python list is the most useful concept which is almost used in most of the apps such that to populate user data on the screen.

In our previous blog we have covered the concept of variables declaration and it’s usage now we will see a little advanced topic over variables i.e., list.

Why advance in the sense in variable we can store a single value at once where as in list we can store multiple values dynamically even.

It’s very much advantageous to make use of list rather than a single variable as it makes our work of accepting or storing data easier.

We can make use of properties like python list methods, list append, pop, length, sort , index, functions, contains, isert. slice and much more on the data we store.

A general way of declaring a list in python is

abc = [ 2, 4, 6, 8, 10 ]

and when you specify abc you can get entire list

You can find the detailed explanation here

List length :

You can find the length of the list based on which you can load data on to the screen and know number of elements available in list so that you can do any operation required.

Length of the list plays a key role in implementation of list, it exactly show the data store inside it so that it will be easier for you to fetch data avoiding out of bound exceptions.

When we try to find the length of the list which we specified above

len( abc )

5

Python list index :

Indexing is a concept where we know the position of the data inside the list it starts from 0 and be like 0, 1, 2 ……. n.

So when you want to fetch data from list you can just pass the index position and get exact data at that position

Value246810
Index (Position)01234
abc( 0 )

2

When you pass the above line you get 2 can go through video tutorial for clear explanation.

Python list append :

Appending data is to add data to existing data in list i..e, by making the current data available adding some more information.

Now try to add / append 12 to the existing list.

abc.append( 12 )

abc

[ 2, 4, 6, 8, 10, 12 ]

Python list pop :

You can remove the data form the list using this method pop, you need to pass the index position such that the data present over that position is removed.

abc.pop( 1 )

[ 2, 6, 8, 10, 12 ]

Value 4 is removed as it is in index position 1.

When you don’t specify any index position the last element in the list is removed using stack principle. Last in First Out.

abc.pop()

[ 2, 6, 8, 10 ]

Python list remove :

You can also directly remove the data by specifying the data itself as parameter instead of position i.e., index.

abc.remove ( 8 )

[ 2, 6, 10 ]

list sort :

Sorting is the mechanism through which we can arrange the data in ascending, descending order and other ways based on requirement.

abc.sort()

And try to find the output.

If you have any query’s in this tutorial on list do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..