The list in python is the most versatile type that contains group of comma-separated items in a square bracket. Each value in a python list is identified by an index. Lists are much more flexible than arrays as they allow us to hold items of different types. Lists also have an advantage of growing as per our program needs which is why many programs use lists over arrays.
Some points to note about Lists :
- Lists are ordered set of items. Python Dictionaries are unordered set of items.
- Elements in a list can have any type.
- It is possible to keep a list within another list (nested list).
- Lists are mutable, that means we can change their elements.
Creating a List
It is very simple to create lists in python. You just have to add the elements in comma-separated form inside square bracket.
As you can see strings should be in between quotes and you don’t have to do anything for the integer type while creating a list. Note that like strings, even lists start at position 0, so be careful when you do range based operations with them.
Accessing a List
It’s very easy to access a particular element in the list or group of elements within particular range in the list (slicing). Take a look at below code :
print "list1", list1;
print "list1[0:5]", list1[0:5];
In the first line we’re accessing the third element from the 0’th position in the list. In second line we’re accessing the elements from 0th position to 5’th position.
Deleting a List
You can delete a particular element in the list and print the updated list values.
This statement will delete the 3rd element from 0th position in the list. You can use the remove() method to delete a particular element from the list.
This will delete the specific element from the list.
Updating a List
You can either directly add new element in the list at specific position or you can use append() method to append new value in the list.
This will replace the current value of the element with the new value. You can also use append() method in the following way.
This will add new element at the end of the list.
You can create a new list by concatenating two lists. Take a look at this code:
list1=[1,2,3,4]; list2=[6,7,8,9]; list3=list1+list2; print list3 [1, 2, 3, 4, 6, 7, 8, 9]
As you can see this will create a new string with merged values from two lists.
Length of a List
You can check the total length of a list if your program requires the total number of elements in the list.
It shows the length of our previous new list3. It has total 8 elements into the new list.
Reversing a List
It is possible to reverse the list and show the reversed elements in the list.
This statement will reverse the content of the list. You’ll see that all the element starting from the 0’th position are now changed to new place.
There are many other operations that you can perform on the lists. This tutorial is just a starting point for those who are new to the lists in python. Hope this tutorial helps solves some of your queries related to lists. Feel free to post your comments, suggestions and flames below. You can also send me tweet @maheshkale if you’re on twitter.