[metaslider id="213"]

List is a datatype in Python which can be print as a list of comma-isolated values between square brackets. Advantage of using a list is that list items need not be of the same type.

Creating a list is as simple as inserting different comma-separated values between square brackets.

subject_list= ['physics', 'chemistry', 1997, 2000];
number_list = [1, 2, 3, 4, 5 ];
char_list = ["a", "b", "c", "d"];

To access a value in list:

subject_list= ['physics', 'chemistry', 'math', 'english'];
number_list = [1, 2, 3, 4, 5 ];

print "Subject: ", subject_list[0]
print "Number : ", number_list [2]

To update a value in a list we can change a particular item by using its index:

list = ['physics', 'chemistry', 'math'];
print "Value at index 1 : "
print list[1]
list[1] = 'History';
print "New value at index 1 : "
print list[1]

To remove an element from list use “del” statement

list = ['physics', 'chemistry', 2018, 2017];
print "Before deleting : "
print list
del list[3];
print "After deleting : "
print list

 

 

List in Python

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *