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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.