Table of Contents
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″];
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
multilist[3] – 2017
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
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
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’]
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]
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
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’]
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’]]
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
print(Languages)
print(Languages.pop(0))
print(Languages)
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’]
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.