### Python List Tutorial

List is a collection in Python. In general a collection is a group of homogeneous or heterogeneous data items treated as a single entity.

When we want to represent a group of data items where duplicates are allowed and insertion order is preserved, then we use list as collection.

List is also called as data structure.

Now we will see some practical fundamentals in Jupyter Notebook IDE.

``````#empty List
x=[]
x
```
```
##### Output: []

``````#list
x=[100,12.34,'python',True,100]
x
```
```
##### Output: [100, 12.34, 'python', True, 100]

``````#Get individual data items from a list through index value
Here in list, the index value that we can specify
from 0,1,2…n-1 and -1,-2,…-n
x[0]

Output : 100```
```
``````x[2]

Output : 'python'```
```
``````x[-1]

Output : 100```
```
``````x[-2]

Output : True```
```
``````x[-5]

Output : 100```
```

``````#length of  a list : the no. of data items present in a list
is called as length of list
len(x)```
```

Output : 5

``````# How much amount of memory reserving for x
import sys
print(sys.getsizeof(x))

Output: 104```
```

here sys module we have imported and there is a getsizeof() when we are invoking then it is returning the size of x, that means how much amount of memory is reserving for x(that is 104).

``````#To get all inbuilt functions present in List
dir(x)```
```

Output:

Here the above are all in-built functions present in list and every function have a specific purpose. Suppose if we don’t know the purpose of a function, then don’t worry. Python provides a help() that you use to get the detail information about a function objective.
For Example-

help(x.append)

Output :

The help() provided the objective of append(), that is append() is used to add a new data item at the end of the list.

``````#Add a new data item
x.append(200)
x

Output: [100, 12.34, 'python', True, 100, 200]```
```
``````#Remove a data item from a list by invoking remove()
x.remove(100)
x

Output: [12.34, 'python', True, 100, 200]```
```
``````#Remove data item by invoking pop()
x.pop()
x

Output: [12.34, 'python', True, 100]```
```

Here the difference between remove() and pop() is the remove() takes an argument that is which data item that we want to remove that we are specifying whereas the pop() taking no argument. The pop() removing last inserted data item.

``````#To copy all data items of a list to another list by invoking copy()
x1=x.copy()
x1

Output: [12.34, 'python', True, 100]```
```

Here all data items of x is copied to another list that is x1.

``````#Release all data items from a list using clear()
x2.clear()
x2

Output: []```
```

``````Let consider another list y as:
y=[10,20,30,40,50]
y

Output: [10, 20, 30, 40, 50]```
```
``````#Merging two list using + operator
x+y

Output: [12.34, 'python', True, 100, 10, 20, 30, 40, 50]```
```

``````#Merging two list using extend()
x.extend(y)
x

Output: [12.34, 'python', True, 100, 10, 20, 30, 40, 50]```
```
``````# To know the occurrence of a data item
x.count(100)

Output: 1 ```
```
``````#Insert a new data item at a specified index using insert()
x.insert(5,400)
x

Output: [12.34, 'python', True, 100, 10, 400, 20, 30, 40, 50, 100, 5]```
```

``````#Reverse of a list using reverse()
y1=[100,200,300]
y1
Output: [100, 200, 300]

y1.reverse()
y1
Output: [300, 200, 100]```
```
``````#Sorting data items in ascending order using sort()
y2=[300,100,400,250]
y2
Output: [300, 100, 400, 250]

y2.sort()
y2
Output: [100, 250, 300, 400]
```
```

``````#To get the index of a data item using index()
y2.index(250)
```
```

Output: 1

Conclusion:

Hi guys, here I gave clarity about the concept of List. I presented here what is List and when we use List and so many operations performed on List. I hope this tutorial will give better satisfaction. Let’s study and enjoy. Thanking you.

Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc