World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
List :
The Python List is a general data structure widely used in Python programs. They are found in other languages,
often referred to as dynamic arrays. They are both mutable and a sequence data type that allows them to be indexed
and sliced.
The list can contain different types of objects, including other list objects.
List methods and supported operators :
Starting with a given list a :
a = [1, 2, 3, 4, 5]
1.append(value) – appends a new element to the end of the list.
# Append values 6, 7, and 7 to the list a.append(6) a.append(7) a.append(7) # a: [1, 2, 3, 4, 5, 6, 7, 7] # Append another list b = [8, 9] a.append(b) # a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9]] # Append an element of a different type, as list elements do not need to have the same type my_string = "hello world" a.append(my_string) # a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9], "hello world"]
Note that the append() method only appends one new element to the end of the list. If you append a list to
another list, the list that you append becomes a single element at the end of the first list.
# Appending a list to another list a = [1, 2, 3, 4, 5, 6, 7, 7] b = [8, 9] a.append(b) # a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9]] a[8] # Returns: [8,9]
2.extend(enumerable) – extends the list by appending elements from another enumerable.
a = [1, 2, 3, 4, 5, 6, 7, 7] b = [8, 9, 10] # Extend list by appending all elements from b a.extend(b) # a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10] # Extend list with elements from a non-list enumerable: a.extend(range(3)) # a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 0, 1, 2]
Lists can also be concatenated with the + operator. Note that this does not modify any of the original lists:
a = [1, 2, 3, 4, 5, 6] + [7, 7] + b # a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]
a = [1, 2, 3, 4, 5, 6] + [7, 7] + b
# a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]
3.index(value, [startIndex]) – gets the index of the first occurrence of the input value.
If the input value is not in the list a ValueError exception is raised. If a second argument is provided, the search is started at that specified index.
a.index(7) # Returns: 6 a.index(49) # ValueError, because 49 is not in a. a.index(7, 7) # Returns: 7 a.index(7, 8) # ValueError, because there is no 7 starting at index 8
4.insert(index, value) – inserts value just before the specified index .
Thus after the insertion the new element occupies position index .
a.insert(0, 0) # insert 0 at position 0 a.insert(2, 5) # insert 5 at position 2 # a: [0, 1, 5, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]
.
5.pop([index]) – removes and returns the item at index .
With no argument it removes and returns the last element of the list.
a.pop(2) # Returns: 5 # a: [0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10] a.pop(8) # Returns: 7 # a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # With no argument: a.pop() # Returns: 10 # a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6.remove(value) – removes the first occurrence of the specified value.
If the provided value cannot be found, a ValueError is raised.
a.remove(0) a.remove(9) # a: [1, 2, 3, 4, 5, 6, 7, 8] a.remove(10) # ValueError, because 10 is not in a
7.reverse() – reverses the list in-place and returns None
a.reverse() # a: [8, 7, 6, 5, 4, 3, 2, 1]
There are also other ways of reversing a list.
8.count(value) – counts the number of occurrences of some value in the list.
a.count(7) # Returns: 2
9.sort() – sorts the list in numerical and lexicographical order and returns None.
a.sort() # a = [1, 2, 3, 4, 5, 6, 7, 8] # Sorts the list in numerical order
Lists can also be reversed when sorted using the reverse=True flag in the sort() method.
a.sort(reverse=True) # a = [8, 7, 6, 5, 4, 3, 2, 1]