Lists in Python

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.

listItems = ["pen", "pencil", "eraser"]
print(listItems)

Output:

['pen', 'pencil', 'eraser']

List can also contain duplicate items. The following program shows a list which has duplicate items. List items are indexed with first item in as [0].

listItems = ["pen", "pencil", "eraser", "pen", "eraser"]
print(listItems)

Output:

['pen', 'pencil', 'eraser', 'pen', 'eraser']

To get the number of items in the list len( ) function is used.

listItems = ["pen", "pencil", "eraser"]
print(len(listItems))

Output:

3

A list can contain items of different types. The following program shows a list with items that are string, integer and boolean.

listItems = ["pen", "2", "True"]
print(listItems)

Output:

['pen', '2', 'True']

list( ) constructor can be used to produce a list. The following program shows list constructor to make a list of items.

listItems = list(("pen", "pencil", "eraser"))
print(listItems)

Output:

['pen', 'pencil', 'eraser']

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.

listItems = ["pen", "pencil", "eraser"]
print(listItems[1])
print(listItems[0])
print(listItems[2])

print(listItems[-1])
print(listItems[-3])

Output:

pencil
pen
eraser
eraser
pen

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. 

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
print(listItems[1:4])
print(listItems[:4])
print(listItems[2:])
print(listItems[-4:-1])

Output:

['pencil', 'eraser', 'sharpener']
['pen', 'pencil', 'eraser', 'sharpener']
['eraser', 'sharpener', 'notebook']
['pencil', 'eraser', 'sharpener']

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]

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
listItems[2] = "calculator"
print(listItems)

Output:

['pen', 'pencil', 'calculator', 'sharpener', 'notebook']

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.

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
listItems[1:3] = ["glue", "stapler"]
print(listItems)

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
listItems[1:2] = ["Diaries", "Notepads"]
print(listItems)

Output:

['pen', 'glue', 'stapler', 'sharpener', 'notebook']
['pen', 'Diaries', 'Notepads', 'eraser', 'sharpener', 'notebook']

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

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
listItems[1:3] = ["Diaries"]
print(listItems)

Output:

['pen', 'Diaries', 'sharpener', 'notebook']

To add an item to a list without replacing any other item, insert( ) method can be used. The following program shows the use of insert ( ) method. 

listItems = ["pen", "pencil", "eraser", "sharpener", "notebook"]
listItems.insert(3, "pencil box")
print(listItems)

Output:

['pen', 'pencil', 'eraser', 'pencil box', 'sharpener', 'notebook']

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.

listItems = ["pen", "pencil", "eraser"]
listItems.append("Sketch pen")
print(listItems)

Output:

['pen', 'pencil', 'eraser', 'Sketch pen']

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.

listItems1 = ["Football", "Hockey", "Cricket"]
listItems2 = ["Chess", "Ludo"]
listItems1.extend(listItems2)
print(listItems1)

Output:

['Football', 'Hockey', 'Cricket', 'Chess', 'Ludo']

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 .

listItems1 = ["Football", "Hockey", "Cricket"]
listItems2 = ("chess", "Ludo")
listItems1.extend(listItems2)
print(listItems1)

Output:

['Football', 'Hockey', 'Cricket', 'chess', 'Ludo']

An item can be removed from a list just by specifying the name and using remove( ) method. The following program shows the use of remove( ) method.

listItems = ["Football", "Hockey", "Cricket"]
listItems.remove("Hockey")
print(listItems)

Output:

['Football', 'Cricket']

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.

listItems = ["Football", "Hockey", "Cricket"]
listItems.pop(1)
print(listItems)

listItems = ["Football", "Hockey", "Cricket"]
listItems.pop()
print(listItems)

Output:

['Football', 'Cricket']
['Football', 'Hockey']

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. 

listItems = ["Football", "Hockey", "Cricket"]
listItems.clear()
print(listItems)

Output:

[]

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. 

listItems = ["Football", "Hockey", "Cricket"]
items = 0
while items < len(listItems):
    print(listItems[items])
    items = items + 1

Output:

Football
Hockey
Cricket

The following program shows list comprehension.  List Comprehension makes efficient, easy to understand and very simple to read the code. 

listItems = ["Football", "Hockey", "Cricket"]
[print(items) for items in listItems]

Output:

Football
Hockey
Cricket

The items of the list can be sorted in ascending order by using the sort( ) method. The following program shows the use of sort( ) method. 

listItems = ["Football", "Hockey", "Cricket"]
listItems.sort()
print(listItems)

Output:

['Cricket', 'Football', 'Hockey']

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.

listItems = ["football", "hockey", "Cricket"]
listItems.sort(key=str.lower)
print(listItems)

Output:

['Cricket', 'football', 'hockey']

Numerical values can be sorted in the list items using the sort( ) method.

listItems = [14, 25, 47, 78, 21, 18]
listItems.sort()
print(listItems)

Output:

[14, 18, 21, 25, 47, 78]

In order to make the sorting in descending order, reverse = True argument is used in the sort( ) method. 

listItems = ["Football", "Hockey", "Cricket"]
listItems.sort(reverse=True)
print(listItems)

listItems = [14, 25, 47, 78, 21, 18]
listItems.sort(reverse=True)
print(listItems)

Output:

['Hockey', 'Football', 'Cricket']
[78, 47, 25, 21, 18, 14]

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. 

listItems = ["Football", "Hockey", "Cricket", "Tennis"]
listItems.reverse()
print(listItems)

Output:

['Tennis', 'Cricket', 'Hockey', 'Football']

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.

listItems = ["Football", "Hockey", "Cricket"]
listItems1 = listItems.copy()
print(listItems1)

listItems = ["Football", "Hockey", "Cricket"]
listItems1 = list(listItems)
print(listItems1)

output:

['Football', 'Hockey', 'Cricket']
['Football', 'Hockey', 'Cricket']

Two lists can be added or joined to make a single list by using the + operator or extend( ) method.

listItems1 = ["Football", "Hockey", "Cricket"]
listItems2 = ["Chess", "Carom", "Ludo" ]

listItems3 = listItems1 + listItems2
print(listItems3)

listItems1 = ["Football", "Hockey", "Cricket"]
listItems2 = ["Chess", "Carom", "Ludo" ]

listItems1.extend(listItems2)
print(listItems1)

Output:

['Football', 'Hockey', 'Cricket', 'Chess', 'Carom', 'Ludo']
['Football', 'Hockey', 'Cricket', 'Chess', 'Carom', 'Ludo']
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments