You are here: Home / Topics / What is List in python

What is List in python

Filed under: Python on 2022-08-23 13:39:30

List is a mutable collection of the elements which belongs to different class of the data type, item in list are separated by comma (,) and list is enclosed in square brackets ([]),.
List are very much similar to the array in c , but the major difference in both list and array in c is that item in list may belong to the different data type but in array all items are of same type.

Example of list - 
list1=[1,2,3] // homogenous list
List2=[1,2,'Mytext'] // heterogeneous list
Both are the valid arrangement while in C programming second arrangement is not possible by array


 

Append in list- Append is a function in list by which we can add the new items at the end of the list. It take one argument and 
syntax is :- list.append(item)

Insert In list - Insert is a function in python which add new item to the list at specified index and it take two arguments , first one is index number and second one is item value.
Syntax is :- list.insert(index,item)

Pop() in list- Pop is a function in python which remove the item from the list at the end side. It similarly work as pop function in stack. And it take no argument.
Syntax - list.pop()

Program-

list1=[1,2,3]
list2=[1,2,"Mytext"]
addlist=list1+list2
print(list1[0])
print(list1)
print(addlist)
print(addlist[0:2])
print(len(addlist))
addlist.append("Newtext")
print(addlist)
addlist.insert(3,1)
print(addlist)
addlist.pop()
print(addlist)

Output:

1
[1, 2, 3]
[1, 2, 3, 1, 2, 'Mytext']
[1, 2]
6
[1, 2, 3, 1, 2, 'Mytext', 'Newtext']
[1, 2, 3, 1, 1, 2, 'Mytext', 'Newtext']
[1, 2, 3, 1, 1, 2, 'Mytext']

About Author:
Y
Yogesh     View Profile
I am Yogesh Sharma. I found this website very useful for all the students. It is a very good website for learning thousands of multiple choice questions. Basically it is like a community website. Everyone can add questions so am I. I will add mcqs in this website to help you.