by Dinesh Thakur Category: Python

A list is a sequence and are most basic mutable type data structures that are responsible for ordered set of values, where each Elements in a list is identified by an index.

List is a most frequently used datatype in Python. Lists are dynamic because that can contain as many variables and handle various types of data at a time.

Lists in python are similar to arrays in Java, which are ordered sets of characters, with the difference that the elements of a list can have any type. Lists and strings and other things that behave like ordered sets are called sequences. Python offers a series of built-in functions and integrated to perform different types of operations methods.

Creating list in python

In Python programming, to create a list, put any number of values (items) inside a square bracket [and], separated by commas, similar to function arguments and they may be of same type or different type. Following is an example of a List in Python. For example:

# empty list
    MyList = []
# list of integers
    MyList = [100, 101, 102, 103]
# list of string
    MyList = [ 'India', 'Canada', 'UK']
# list with mixed datatypes
   MyList = [100, "India", 103]
# nested list
    MyList = ["UK", [100, 200, 300]]

Accessing Values in Lists

There are lots of ways to access the elements of a list.

Index Operator []

The syntax for access single element of a list is the same as you would do in an array. The expression inside the square brackets [] specifies the index. List indexes are zero based; that is, they range from 0 to len(MyList)-1. Index value can be used like a variable, so it's called an indexed variable. For example

MyList1 = [100, 101, 102, 103]
MyList2 = ['India', 'Canada', 'UK']
print (MyList1[0])
# Any integer expression can be used as an index
print MyList2[1:3]
After execution above code, it produces the following result.

['Canada', 'UK']


Negative indexing

Python allows list items can be accessed with positive or negative index. The index value -1 refers to the last element of index, or -2 can be second last element and so on.

MyList = ['India', 'Canada', 'UK']
MyList [-1]
 MyList [-3]
Here is an example of the python list that cannot be accessed beyond the upper boundaries.
File "<stdin>", line 1, in <module>
IndexError: list index out of range
As seen above, when we are tried to access MyList index 5, an IndexError was thrown.

List Slicing [start : end]

We can access a list range by using the slicing operator (:).

MyList = ['e','c','o','m','p','u','t','e','r']
MyList [2:5]  
['o', 'm', 'p']
MyList [:-5]  
['e', 'c', 'o', 'm']
 MyList [5:]    
['u', 't', 'e', 'r']
MyList [:]    
['e', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r']

Deleting or Removing Elements from the List

Python provides the remove() or del() method through which we can delete one or more elements from a list. we use the 'del' statement when you want to remove elements by index, or a range of indices.

MyList = ['US', 2015, 'India' , 2011];
print MyList
# delete one item
   del MyList[2];
# delete multiplt items
   del MyList[1:3]  
# delete entire list
    del MyList       
print "After Deleting the List : "
print MyList

We can use remove() method deleting first matching element in the list.
MyList = ['US', 2015, 'India' , 2011];
print MyList
['US', 2015 , 2011];

Members list

Logical operator IN that tests whether an element is a member of a sequence and it also works with lists and other sequences:

>>> Fruit = [‘Apple’, 'Gava', 'Orange', 'Banana']
>>> ’Apple’ in Fruit
>>> ’Grapes’ in Fruit

Since "Apple" is a member of the Fruit list in the operator returns true. Once Grape is not on the list, in return false.

We can also use in combination with not ping to test whether an element is not a member of a list:

‘‘Grapes‘‘ not in Fruit


Python List Methods

The list class methods for manipulating a list are tabulated below.

       Python List Methods

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.