In python, we can do plenty of stuff with lists, these are easily maintainable and changeable as per our requirement. As these are sequence data type like array in any other language C,C++ or java, but python list is more flexible which make it more it power powerful than any other arrays.

In python list, we cam store any type of items in list means items stored could be different type or are not fixed, you could change it anytime between your program as compared to any other array like in C which is fixed in size.

let’s work on Python list to understand how it work and how we can use them within our program.

Creation of List

Creating List is easy and flexible, like we can create it in below code.

>>> lst=[1,"two",3,"four",5,"six"]
>>> type(lst)

So for creating one list in python, we have to mention items within square brackets like above.

Access items in List

>>> lst[2]
3

>>> lst[2:4]
[3, 'four']

>>> lst[-4:-2]
[3, 'four']

>>> lst[-5:]
['two', 3, 'four', 5, 'six']
>>> lst[1:]
['two', 3, 'four', 5, 'six']

So as we tried to access some items in list with index numbers. We can also used to access ranges of items indexing start from 0 (left to right) and from -1 (right to left)

While accessing list items, we should care using indexing. From left to right and right to left is different like below.

>>> lst[0]
1
>>> lst[-1]
'six'

But what if we have list inside list like below

>>> lst1=[lst,7]
>>> lst1
[[1, 'two', 3, 'four', 5, 'six'], 7]

In these case, we could access them with same simple rule as we did above.

>>> lst1
[[1, 'two', 3, 'four', 5, 'six'], 7]
>>> lst1[0][1]
'two'

Sometime things get complex when we start seeing them as complex, So better try to compute it simple before assume it complex.

Update List

At any point of time in program we could update or change list items as did in below code.

>>> lst
[1, 'two', 3, 'four', 5, 'six']
>>> lst[1]=2
>>> lst
[1, 2, 3, 'four', 5, 'six']

In above code we changed one list items, we could also add another items as well.

>>> lst
[1, 2, 3, 'four', 5, 'six']
>>> lst.append(7)
>>> lst
[1, 2, 3, 'four', 5, 'six', 7]

In above we used in-build functions of python which has designed to append any items in next index value of List.

Removing of List items

We can do this in many ways, as in python we have many ways are available achieve same thing. Let’s try how we could remove list items.

>>> lst
[1, 2, 3, 'four', 5, 'six', 7]
>>> lst.pop()
7
>>> lst
[1, 2, 3, 'four', 5, 'six']

In Above code we could only remove last element of list, which make it bit insufficient for sometime. We have another way to target deletion of list items.

>>> lst
[1, 2, 3, 'four', 5, 'six']
>>> lst.remove(2)
>>> lst
[1, 3, 'four', 5, 'six']

In above way, we can see we able to delete any required item in list. But what if we only know index value need to delete.

>>> lst
[1, 3, 'four', 5, 'six']
>>> del lst[1]
>>> lst
[1, 'four', 5, 'six']

In above code we able to delete specific index value from list.

So now we know we have many ways to work with python list, but we have many other ways to work with them which not possible to mention in one post, will try to make another post for same to cover many of them.