Python List

Definition of List?

List is a sequence of elements/objects separated by commas inside a square bracket[].

In other programming language it is an array.

List is a mutable means we can change the elements inside the list.

Example:

my_list = ['India','15','august']

'my_list'→ is the name of the list
['India',15,'august'] → Elements inside the list


Lets do some practical:

Go to your command prompt.

>> my_list = ['India','15','august']
>>> print (my_list)
['India', '15', 'august']
>>>

Now we know what is list but we don’t know how to use that list and how to play with the list to get our expected output, so lets read below what are the important operations we can use in list.

List Operations:

  1. Create a list
  2. Adding element inside the list
  3. Accessing Elements from the list
  4. Deleting element from the list
Create a list :
    •  Before goin g to use list first you have to know how to create an empty list? or how to create a new list? 
    • To create an empty list we have to use below format. where you have to mention your list name here it is ‘mylist’ and the ‘=’ sign then two square bracket .
    • >>> mylist = [  ]
      >>> print (mylist)
      [  ]
      >>>

So now your list has been created without any elements inside this.So if you want to add any element inside the list then just write your list name i.e any name you wants here i am using the same name for your understanding then = sign and the square bracket and inside the square bracket you can add any elements you wants to put , so lets see below what i did to do the same.

Adding element inside the list:

 

>>> mylist = ['PyGround','python','center']
>>> print (mylist)
['PyGround', 'python', 'center']
>>>

In above we added elements inside an empty list.So now the questions is if you want to add more elements inside the existing list what you will do.

So in python to do this there is an method call ‘append’ ,which will add the elements at the end of the list, lets see below how its.

>>> mylist = ['PyGround','python','center']
>>> print (mylist)
['PyGround', 'python', 'center']
>>> mylist.append('is best')
>>> print (mylist)
['PyGround', 'python', 'center', 'is best']
>>>

If you noticed the append method , there i just added a string ‘is best’ and finally it added to my existing list and the final list is  [‘PyGround’, ‘python’, ‘center’, ‘is best’].

To use the append method  just write your list-name.append(‘your data’), that’s all . you can add any type of data  inside a list , also you add list inside a list, will tell this latter just follow this you will be a master on that.

Now you created a list but what is the use you are not accessing the elements or retrieving the elements inside the list , so here is how you can do.

Accessing Elements from the list:

Before going to access the elements from the list you have to know the indexing and how the elements are stored inside a list in memory .Know more about indexing.

Using index you can access every elements,

Syntax to access element through index is:

list-name[index]

Example:

>>> mylist = ['PyGround','python','center','is best']
>>> mylist[1]
'python'
>>> mylist[-1]
'is best'
>>>

Lets understand the example which is very simple.

As you know here your list name is “mylist” and index is 1.

Above we used index 1 with a squire bracket  to access first element and -1 to access the last elements from the list,we got the output “python” and “is best” accordingly.

Access elements using slicing :

When you are using a colon : inside a square bracket  that is called slicing in list.

Example :

 

>>> mylist = ['PyGround','python','center','is best']
>>> mylist[:2]
['PyGround', 'python']

In above i used [:2] to access the elements from the list.

Let’s understand what  is this slicing? 

When we used [:2] means 0th index and 1 index element which is “PyGround” and “python” but it excludes the index 2 , which means starting from 0th till to that slicing index which is 2 here.

Don’t look the answer  and try what is the output of the below code.

>>> mylist = ['PyGround','python','center','is best']

>>> mylist[1:3]

So here it will start from 1 and end at 3 means up-to 3rd index .

The output is below: is the element which have 1 and 2 index.

['python', 'center']

Negative index slicing:

We can use negative index to retrieve the elements from the list as we used positive index.

Below example I am explaining about  negative indexing.

Example:

>>> mylist[-2:]
['center', 'is best']
>>>

in above you will get -2 and -1 index  from  the list.

It is similar to positive index rule . lets understand one more example to understand better.

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> mylist[-3:-1]           ↓          ↓
['python', 'center']       -3         -2

If you look the above arrow and the output where it will start from -3 and end at -2 but exclude -1.

Below you can see more example to understand.

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> mylist[-3:-1]
['python', 'center']
>>> mylist[:-3]
['PyGround']
>>> mylist[:-2]
['PyGround', 'python']
>>>
Deleting element from the list.

So now we know how to add elements inside a list and access the element from the list.

Now we will learn how to remove or delete elements from the list.

Below are the three methods you can use to achieve this.

  1. del
  2. remove
  3. pop

 

1-del

Use of “del” keyword to delete an element from the list.

Example:

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> del mylist[1]
>>> mylist
['PyGround', 'center', 'is best']
>>>

In above you saw “del” used before the list name and 1 which is the index of that  element  inside the square bracket. now when you are printing the list “python” is not present , so it is deleted from that same

Similarly we can delete multiple elements from the list .

Example:

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> del mylist[1:3]
>>> mylist
['PyGround', 'is best']
>>>

Here you know how  you can delete multiple elements from the list.

Deleting entire list.

Example:

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> del mylist
>>> print (mylist)
Traceback (most recent call last):
File "<stdin>", line 1, in &lt;module>
NameError: name 'mylist' is not defined
>>>

In above example you can see the entire list is deleted and when you are again trying to access that list it is not present that’s why the error came as “Nameerror”.

2-Remove

Use of “remove()” method to delete an element from the list.

>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> mylist.remove('center')
>>> print (mylist)
['PyGround', 'python', 'is best']
>>>

Lets understand the above example here I used ‘remove’ method to delete element from the list .So as you observed remove method takes the list element as the argument not the index.

3-Pop

Use of “pop()” method to delete elements from the list.

  • pop() method takes an index to remove the element if that index is not provided it will remove last element from that list and return that element.

Example:

>>> mylist = ['PyGround', 'python', 'center', 'is best']
>>> mylist.pop(1)
'python'
>>> print (mylist)
['PyGround', 'center', 'is best']
>>> mylist.pop()
'is best'
>>> print (mylist)
['PyGround', 'center']
>>>

Let’s understand the above example, here first i used “mylist.pop(1)” where 1 is the index of that list and the element is ‘python’, which is now deleted and returned as the output.

Now the list is  [‘PyGround’, ‘center’, ‘is best’] , so then  I used “mylist.pop()”, so here we are not provided any index anything but it deleted last element from the list and return the last element as the output.

So after all operation the final list is [‘PyGround’, ‘center’] as  you saw above.

Now you understood Python List with our simple explanation.

We are always making our article so simple so that anybody can understand easily and learn.

If you like this article then share with your friends and colleagues, comments in the below section.

Some useful links in Python:

If you wants to join our online  python crash course then contact us    

Be a master in python then contact us and enrolled our python crash course.

Learning is so simple with PyGround .

Happy Learning.

5 thoughts on “Python List”

  1. At the beginning, I was still puzzled. Since I read your article, I have been very impressed. It has provided a lot of innovative ideas for my thesis related to gate.io. Thank u. But I still have some doubts, can you help me? Thanks.

  2. Read reviews and was a little hesitant since I had already inputted my order. or it may be but thank god, I had no issues. significantly received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans https://www.realjordansshoes.com/

  3. I may need your help. I’ve been doing research on gate io recently, and I’ve tried a lot of different things. Later, I read your article, and I think your way of writing has given me some innovative ideas, thank you very much.

  4. I may need your help. I’ve been doing research on gate io recently, and I’ve tried a lot of different things. Later, I read your article, and I think your way of writing has given me some innovative ideas, thank you very much.