Lists are data types which are used to store collection of data. These data can be of different types like integer, float or string. List is created by using a square bracket.
The following program shows a list which contains some items.
Items of the list can be accessed with index number which starts with zero. Negative indexing starts with index number [-1] from the end of the list with the last item.
A range of list items can be specified. The search starts with the specified index number and ends with one before the specified index number as indexing of the list starts with zero. When there is no starting index the range starts from the first item. When there is no index number at the end, the range ends in the last. Negative indexing makes search from the end of the list. The following program shows a list of items with the specified range. Here first index is included and ending index is excluded in range.
A specified item can be checked in the list with the ‘in’ keyword and ‘if’ statement. The following program shows to check if an item is there in the list.
listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
if "pencil" in listItems:
print("pencil is in the list")
Output:
pencil is in the list
A list item can be changed by referring to the index number. The following program shows the change in the list item at index number [2].
A range of list items can be changed by indicating the index number. The last index number is excluded. If more items are replaced than the specified range, the list items are moved. The following program shows change in the range of list items and addition of extra item than the specified range.
If number of items is less than to be replaced, the new item will be inserted at the specified index in the list and other items in the range will be removed. The following program shows replacing item at index [1], while its range is up to index [3].
An item can be inserted at the end of the list with append( ) method. The following program shows insertion of an item in the list using the append( ) method.
The items from one list can be added to the items of the another list using extend( ) method. The items are added to the end of the list items. The following program shows the use of extend( ) method.
The extend( ) method can also be used to add items from another data types like tuples, sets or dictionaries. The following program shows addition of items of a tuple .
An item in the list can also be removed by using pop( ) method. The pop( ) method removes the item in the list with index number. However, if no index number is specified than last item is removed. The following program shows the use of pop( ) method.
An item at a specified index in the list or the list can be deleted by using the del keyword. The following program shows the use of del keyword and removes the second item and the list.
listItems = ["Football", "Hockey", "Cricket"]
del listItems[1]
print(listItems)
listItems = ["Football", "Hockey", "Cricket"]
del listItems
output:
['Football', 'Cricket']
The clear( ) method deletes all the items in the list. The following program shows the use of clear( ) method.
The list items can be printed by using the for loop. The following program shows for loop to print the list items.
listItems = ["Football", "Hockey", "Cricket"]
for items in listItems:
print(items)
listItems = ["Football", "Hockey", "Cricket"]
for items in range(len(listItems)):
print(listItems[items])
Output:
Football
Hockey
Cricket
Football
Hockey
Cricket
While loop can also be used to print the list items. The following program shows the use of while loop to print the list items. Here, len( ) function is used to get the length of the list.
The sort( ) method sort the list items as upper case letter first. If a list contains items with both lower case letter and upper case letter it will sort upper case letter first and then lower case letter. The key( ) function can be used to sort the list items in case in-sensitive. The following program shows the use of key( ) function, str.lower to make the sorting case in-sensitive.
The items in a list is in reverse order on using the reverse( ) method. The following program shows the use of reverse( ) method to reverse the list items.
The items in a list can be copied to another list by using the copy( ) and list( ) method. The following program shows the use of copy( ) and list( ) method.