Lists in Python
Lists in Python
Lists are used to store data in a single variable. Lists in python is same as arrays , the only difference is arrays store only homogeneous type of elements (say all elements must only be integers , float , string) but in lists it stores heterogeneous type of elements. Lists are mutable and indexed where index starts from zero. Keyword used for lists is “list”
Python Lists
In Python, a list is a versatile and widely used data structure that allows you to store and manipulate collections of items. Lists are ordered, mutable (modifiable), and can contain elements of different types. They are defined using square brackets [ ] and can hold zero or more items separated by commas.
Example lists: Lst1=[“a”,”b”,”c”,”d”]
Lst2=[1,2,3,4,5,6]
Lst3=[True,”learn python” ,”with”,”PrepInsta”,440,403]
Properties of lists
- Lists are indexed
- Lists are mutable
- Lists allow duplicates
- Lists can store heterogeneous datatypes
- Lists can be used as nested format
Declaration of lists in python
An empty list can be declared as two types
- using keyword list()
- using square braces [ ]
L1=list() L2=[] print("List L1 is :",L1) print("List L2 is :",L2)
List L1 is : []
List L2 is : []
List Functions
Some of the important functions in python are
- len
- append
- insert
- count
- index
- sort
- reverse
- pop
Len:
len function is used to find the length of the list. It returns integer.
Syntax: len(Lst)
append:
append means adding . append function is used to add elements to the list. Generally it adds elements to the end of the list.
Syntax: Lst.append(element)
insert:
insert function is also used for same purpose as append. But it adds element at the desired place. It takes two arguments , element and index where to be inserted.
Syntax: Lst.insert(index,element)
count:
count function returns the count of particular element that is passed as arguments.
Syntax: Lst.count(element)
index:
index function returns the index of the first occurred particular element from the list.
Syntax: Lst.index(element)
sort:
sort function sorts the list in ascending order
Syntax: Lst.sort(reverse=True/False,key=func)
here reverse and key is optional . If reverse is True then it sort in descending order.
key is parameter on which sorting criteria depends.
reverse:
reverse function will reverse the order of the list.
Syntax: Lst.reverse()
pop:
pop will delete the elements from the list and stores it in a variable. It works on index
Syntax: x=Lst.pop(index_of_element)
Lets check a program using all the above functions
Lst=list() n=5 print("Give inputs to the list :") for i in range(n): a=input() #adds elements to the list Lst.append(a) #insert element at index 1 Lst.insert(1,"bat") print("List after inserting :",Lst) print("Length of the list :",len(Lst)) print("count of apple in the list :",Lst.count("apple")) #sorts in ascending order Lst.sort() print("List after sorting :",Lst) Lst.reverse() print("List after reversing :",Lst) x=Lst.pop(4) print("Popped element ",x) print("Final list :",Lst)
Give inputs to the list :
apple
cat
dog
eye
apple
List after inserting : ['apple', 'bat', 'cat', 'dog', 'eye', 'apple']
Length of the list : 6
count of apple in the list : 2
List after sorting : ['apple', 'apple', 'bat', 'cat', 'dog', 'eye']
List after reversing : ['eye', 'dog', 'cat', 'bat', 'apple', 'apple']
Popped element apple
Final list : ['eye', 'dog', 'cat', 'bat', 'apple']
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment