List in Python | Python Tutorials | Python Examples
×

List in Python

3114

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]

Output
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.

Example
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]

Output
value of index2:
java
modified value of index2:
301


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:

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

Output
['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'];
List5.remove('c++');
print List5
List5.remove('visualc++');
print List5

Output
['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

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

Output
['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.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments