Write a program to demonstrate list in python
List : It is as same as arrays , it contains heterogeneous datatypes .
eg: a=[1,2,"a"]
a[0] =0
print(a)
a=[0,2,"a"]
Functions in list
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Program
# Creating a List List = [] print("empty List: ") print(List) |
---|
Output
empty List:
[]
# Creating a List with the use of a String List = ['GoEduHub.com'] print(List) |
---|
Output
['GoEduHub.com']
# Creating a List with the use of multiple values List = ["Go", "Edu", "Hub",".Com"] print(List[0]) #accesing values of list print(List[1]) print(List[2]) print(List[3]) |
---|
Output
Go
Edu
Hub
.Com
# Creating a Multi-Dimensional List By Nesting a list inside a List List = [['Go', 'Edu'] , ['hub.com']] print(List) |
---|
Output
[['Go', 'Edu'], ['hub.com']]
# Creating a List with the use of Numbers List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print(List) # Removing element using pop() method List.pop() #removes last element print(List) |
---|
Output
[1, 2, 4, 4, 3, 3, 3, 6, 5]
[1, 2, 4, 4, 3, 3, 3, 6]
# Print elements from beginning to a pre-defined point using Slice SList = List[:-6] print(SList) # Print elements using Slice operation Sliced_List = List[3:8] print(Sliced_List) # Print elements from a pre-defined point to end SList = List[5:] print(SList) # Printing elements from beginning till end SList = List[:] print(SList) # Creating a List with mix values List = [1, 2, 'Go', 4, 'Edu', 6, 'Hub'] print(List) # Printing elements in reverse using Slice operation Sliced_List = List[::-1] print(Sliced_List) |
---|
Output
[1]
[3, 3, 3, 6]
[3, 6]
[1, 2, 4, 3, 3, 3, 6]
[1, 2, 'Go', 4, 'Edu', 6, 'Hub']
['Hub', 6, 'Edu', 4, 'Go', 2, 1]
# Removing elements using Remove() method List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] print(List) List.remove(5) print(List) List.remove(6) print(List) |
---|
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
# Removing elements from List using iterator List =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] for i in List: print(i) |
---|
Output
1
2
3
4
5
6
7
8
9
10
11
12
For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here