Python Lists
Python Lists
In python, List is a collection of an ordered sequence of items, and you can store the values of different data types.
In python, you can create the list by enclosing the list items within brackets [ ] and the list items must be separated by commas.
Following is the example of creating a list with different types of items in python.
When you execute the above python program, you will get the result as shown below.
Following is the example of accessing the elements from the list in python.
When you execute the above python example, you will get the result as shown below.
10
30
Tutlane
In python, you can also use negative indexing to access list elements starting from the end. The index value -1 will refer to the last item, -2 refers to
the second-last item, and so on.
Following is the example of accessing elements from a list using negative indexing in python.
When you try to execute the above python list example, you will get an index out of range exception like as shown below.
Following is the example of accessing the specified range of elements from the list using a range of indexes in python.
If you observe the above result, the list items slicing started from the index position 1 and stopped one item before the end index position 4.
It’s optional to define the starting and ending index positions in python to get the range of values from the list. If you skip mentioning the starting
index position, the range will start from the first item the same way if you skip mentioning the ending index position, the range will continue to the
end of the list.
Following is the example of getting the range of list items with or without specifying the starting and ending index list items.
Following is the example of changing the particular list item value using index number in python.
Following is the example of adding items to the list using the append() method in python.
Following is the example of inserting the items in the list at specified index positions using the insert() method in python.
The del keyword is useful to remove list items at specified index positions or delete the list completely.
Following is the example of using the del keyword to delete the list items at specified index positions in python.
Following is the example of removing the list items based on the value using the list remove() method.
Following is the example of using the list pop() method to remove list items.
List Length
In python, to count the number of items in the list, use the len() function.
Following is the example of using len() function in python to get the list length/size.
lst = []
count = len(lst)
if count > 0:
print("List size: ", count)
else:
print("List is empty")
The above list example will return the result as shown below.
List is empty
Join or Concatenate lists
In python, the + operator is useful for joining or concatenating multiple lists and returning a new list with all the elements.
Following is the example to verify whether the particular exists in the list or not using in operator in python.
Following is the example of looping through the list items using for loop in python.
10
20
30
tutlane
learn
Python Copy List
In python, you can copy a list using the list copy() method or list() method, but directly copying the list like list2 = list1 is not allowed because
the list2 will reference list1. So, the changes you made in list1 will automatically reflect in the list2.
Following is the example of using the list copy() method to copy a list in python.
====Before Copy====
List1: [10, 20, 30, 'tutlane', 'learn']
List2: []
====After Copy====
List1: [10, 20, 30, 'tutlane', 'learn']
List2: [10, 20, 30, 'tutlane', 'learn']
Following is the example of using a built-in list() method to copy a list in python.
====Before Copy====
List1: [10, 20, 30, 'tutlane', 'learn']
List2: []
====After Copy====
List1: [10, 20, 30, 'tutlane', 'learn']
List2: [10, 20, 30, 'tutlane', 'learn']
Following is the example of reversing the list items using the reverse() method in python.
Following is the example of sorting the list elements in ascending order using the sort() method in python.
The above python list example will return the result as shown below.
Following are the built-in list methods available in the python programming language.
Method Description
extend() It is useful to add elements of one list to the end of another list.
count() It will return the number of elements with the specified value.