Table of Contents
List in a Python Programming language
In this tutorial, we will discuss the list in python programming language.
alphabets=[“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”];
List may be contain multiple type of data
multilist=[“physics”,”chemistry”,”1998″,”2017″];
1. Accessing value in a list
We can accessing data in list using index indice of list
alphabets=[“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”];
multilist=[“physics”,”chemistry”,”1998″,”2017″];
We can seperate every element of list using follwing code
print(Languages[0]); – Seperate 0 index element from list of Language
print(Numbers[2]); – Seperate 3 index element from list of Numbers
print(multilist[3]); – Seperate 4 index element from list of multilist
the output of the above program
multilist[3] – 2017
2. Updating list elements
We can update a single or multiple elements of the list using assign operator given below
print(“New value in the list of languages available in index 2:”)
Languages[2]=”CSS”; // insert to list of Languages
Output above program
Value in the list of languages available in index 2:
java
New value in the list of languages available in index 2:
New value in languages available in index 2:
CSS
3. Delete Elements of list
To remove the list element, we can use del statement or remove() method
multilist=[“physics”,”chemistry”,”1998″,”2017″];
print(multilist)
del(multilist[2]) // using del statement
print(multilist) //print multilist after del index [2] element
multilist.remove(multilist[2]) // remove index[2] element from new list
Output of above program
[‘physics’, ‘chemistry’, ‘1998’, ‘2017’]
[‘physics’, ‘chemistry’, ‘2017’]
[‘physics’, ‘chemistry’]
4.Insert Elements of the list
We can insert elements of list using insert() method
Numbers=[“45”, “67”, “89”, “76”, “54”, “43”, “32”, “91”]; // list
print(number)
Number.insert(3,100) // use insert method
print(number)
Output above code
[’45’, ’67’, ’89’, ’76’, ’54’, ’43’, ’32’, ’91’]
[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100]
5.Count Elements of the list
We can count element of the list using count() method
Number=[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100];
print(Number)
Number.count(‘100’) // use count method
print(Number)
Output of above program
[’45’, ’67’, ’89’, ‘100’, ’76’, ’54’, ’43’, ’32’, ’91’, ‘100’]
2
6.Reverse Elements of the list
We can change reverse order element of the list using reverse() method
Number=[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100];
print(Number)
Number.reverse()
print(Number)
Output above program
[’45’, ’67’, ’89’, ‘100’, ’76’, ’54’, ’43’, ’32’, ’91’, ‘100’]
[‘100′, ’91’, ’32’, ’43’, ’54’, ’76’, ‘100’, ’89’, ’67’, ’45’]
7.Append Elements of list
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
print(Number)
Number.Append([“css”,”Js”,”ruby”])
print(Number)
Output above program
[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’, [‘css’, ‘Js’, ‘ruby’]]
8.pop Elements of list
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
print(Languages)
print(Languages.pop(0))
print(Languages)
[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
php
[‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
9.sort Elements of list
We can sort object of a list
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
print(Languages)
Languages.sort();
print(Languages)
Output
[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
[‘Asp.net’, ‘C’, ‘C++’, ‘html’, ‘java’, ‘jquary’, ‘php’, ‘vb.net’]
10.index Elements of list
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
print(Languages)
print(Languages.index(“java”));
Output
[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
2
Related post