List in Python

by Monika Dadool 24-Jan-19

2 0 83

The sequence is the most common data structure used in python programming language. Every element is assigned number, Index or position. The first position is assigned zero, and the second is one and continued till the last element. With all sequence types, various operations can be performed for membership such as slicing, multiplication, indexing and adding. It is not mandatory for the list to contain the same type of elements. In python list is used if we have different values rather it is number or strings and want to group them together. The list contains a collection of various elements between square brackets which is ordered and changeable.

One can easily create a list with the use of some of the comma for separating values between square brackets.

Example of list
list6 = ['c', 'c++', 'java', 'visualc++', 'core java'];
list7 = [11,22,33,44,55,66,77];

Accessing lists:
For accessing values in the lists, the square brackets are used along with index for slicing.

Example of a list with the help of small program
list6 = ['c', 'c++', 'java', 'visualc++', 'core java'];
list7 = [11,22,33,44,55,66,77];
print "list6[3]:", list6[3]
print "list7[2:5]:", list7[2:5]

list6[3]: visualc++
list7[2:5]: [33, 44, 55]

Modification of lists in python
Single and multiple elements of the list can be updated with the slice on the left-hand side of an assignment operator. With append() method, new elements can be integrated into a list.

list = ['c', 'c++', 'java', 'visualc++', 'core java'];
print "value of index2: "
print list[2]
list[2] = 301;
print "modified value of index2:"
print list[2]

value of index2:
modified value of index2:

How to delete elements from the lists
To delete elements from the list, two methods are used i.e. Remove statement and del statement depending upon the situation. There can be two situations that can arise while deleting elements that can be either when exact element that to be deleted is known or it is unknown and other situation may be when the position of the elements to be deleted is known. del statement is used when anyone one wants to del through index and remove statement is used by writing specific element's name along with remove statement that will be explained through examples given below:

list5 = ['c', 'c++', 'java', 'visualc++', 'core java'];
print list5
del list5[3]
print "the value of modified list5: "
print list5

['c', 'c++', 'java', 'visualc++', 'core java']
the value of modified list5:
['c', 'c++', 'java', 'core java']

Remove statement
List5 = ['c', 'c++', 'java', 'visualc++', 'core java'];
print List5
print List5

['c', 'java', 'visualc++', 'core java']
['c', 'java', 'core java']

Other operations in the list
Similar to string, we can perform basic operations such as concatenation, repetition, iteration effortlessly in the list by simply using the operators such as +,* but the modified output will not be a string, it will return list only.

Following is the simple example of performing basic operation in list with the use of + operator

List5 = ['c', 'c++','java','visualc++','core java'];
List6 = ['c', 'c++','java','visualc++','core java'];
List9 =List5+List6
print List9

['c', 'c++', 'java', 'visualc++', 'core java', 'c', 'c++', 'java', 'visualc++', 'core java']

List contain various number of builtin list functions and methods used in python programming languages for making python programmer life more simpler i.e.  maxlist,lenlist,list.appentobj1 and many more.



Leave a Reply