A Python list is a sequence of values. It is the most basic mutable type data structure responsible for an ordered set of values and a versatile collection of data objects. An index identifies each item or sometimes in a list.
The list is the most frequently used datatype in Python. Lists are dynamic because they can contain as many variables and handle various data types 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.
We’ll be covering the following topics in this tutorial:
How to Create Lists in Python
In Python programming, to create a list, place any number of items (elements) inside a square bracket [and], separated by commas, similar to function arguments and they may be of the 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]]
So that you may see a list termed as ‘myList’ was made with a few elements.
Lists in python are not any indexed. This implies you can access individual elements in a list as you’d do within an array. Here’s an example:
Whenever you Make a List in Python, you will need to make a difference between:
Lists that include strings, in which each item within the list will set within quotations:
ListName = [‘Item1′,’Item2′,’Item3’,].
Lists that contain numeric values, where each item will not place within quotes:
ListName = [Item1, Item2, Item3,….].
Lists that will have a combination of strings and numeric values.
Accessing items in a python list
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. The index value can be used as 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. 100 ['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 the index, or -2 can be second previous element and so on.
MyList = ['India', 'Canada', 'UK'] MyList [-1] 'UK' MyList [-3] 'India' Here is an example of the python list that cannot be accessed beyond the upper boundaries. MyList[5] 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 python 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 items 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];
MyList.remove(‘India’)
print MyList
[‘US’, 2015 , 2011];
Members list in python
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 True >>> ’Grapes’ in Fruit False
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 True
Python List Methods
The list class methods for manipulating a list are tabulated below.