Page Content

Tutorials

What Are Nested Lists List Functions and List Methods In DSA

Nested Lists List Functions and List Methods

Nested Lists List Functions and List Methods
Nested Lists List Functions and List Methods

Data can be stored in a sequence using Python lists, which are flexible, built-in linear data structures. They have a number of important characteristics, including as being changeable (whose elements can be changed after creation), ordered (things are stored in a certain sequence, accessible by index), and able to hold items of various data kinds, including combinations of strings, numbers, and other objects. In Python, lists are essential container types that are frequently used as the foundation for more intricate data structures. Python lists are implemented internally as array-based sequences that store references to their elements in a block of memory that is sequential.

Nested Lists

  • A list that is an element inside another list is called a nested list. When it comes to encoding multidimensional data sets in Python, such matrices or two-dimensional arrays, this structure is quite helpful. A 2D array, for instance, can be represented as a list in which every member is a list that represents a row.
  • Using many sets of square brackets to access elements in a nested list is simple. Data[row index][column index] gives you access to a certain value for a matrix data.

Code Example

data_list = [18, 22, 24, 25, 60]
print(len(data_list))
print(min(data_list))
print(max(data_list))
print(sum(data_list))
sorted_list = sorted(data_list)
print(sorted_list)
print(data_list)

Output:

5
18
60
149
[18, 22, 24, 25, 60]
[18, 22, 24, 25, 60]

Using the multiplication operator to initialise nested lists is a typical mistake. Data = [ * c] * r, for example, will produce a list in which each of the r inner lists is a reference to the same list object rather than a separate copy. If you update an entry in one “row,” it will affect all rows because they all point to the same underlying list, which can have unexpected consequences.

List Functions (Built-in)

Without changing the original list, Python offers a number of built-in methods that can work with lists (and other iterable types) and return data or a new object.

  • len(list): Gives back how many items (length) there are in the list. The time required for this operation is constant (O(1)).
  • The smallest entry in the list is returned by the min(list) function.
  • max(list): Provides the list’s largest element.
  • sum(list): Provides the total of the list’s numerical components. If there are non-numeric types, a TypeError is raised.
  • sorted(iterable): A new list with every member from the original iterable in sorted order is returned by the function sorted(iterable). The original list is not altered by this function.

List Methods

The dot (.) operator is used to invoke list methods, which are functions that are part of the list objects themselves (e.g., my list.method()). While some methods are mutators or update methods (they alter the state of the object), others are accessors (they return information without altering the object’s state).

The following list approaches are frequently used:

Append(element): Appends the element to the list’s end. Although enlarging the underlying array occasionally could result in an O(n) worst case, this is an amortised O(1) operation, which means it usually takes constant time.

Extend(iterable): To append every element from an iterable object to the end of an existing list, use Python’s extend() method. This implies that x.extend(y) will change x by adding each element from y to its end if you have a list x and an iterable y. It’s important to know that the elements are referenced in the list rather than being duplicated.

Insert(index, element): Element at the given index is inserted using insert(index, element). Because it could involve shifting entries, this operation has a worst-case running time of O(n-k+1), where n is the length of the list and k is the insertion index.

Pop(index=None): Takes the element at the given index and returns it. It eliminates and returns the final element if no index is supplied. pop() is an amortised O(1) operation that removes the final element. O(n-k) is pop(k).

Remove(value): Eliminates the list’s first instance of the given value. Because it might have to search the full list, this operation has an O(n) worst-case execution time.

Del statement: This Python statement, which isn’t a method, can be used to delete the entire list object or to remove elements by index or slice.

Reverse(): Elements are arranged in the opposite order from their initial sequence using the Reverse() method or, more generally, the concept of reversing a sequence. The built-in Python reverse() method for lists and arrays usually carries out an in-place reversal, which means that it makes no new changes to the original list or array.

Sort(): Sorts the list’s items in-place, making changes to the original list. Ascending is the default sort order. Usually, this process takes O(n log n) time.

Count(value): A basic Python function that may be applied to tuples, lists, and strings, among other sequence kinds, is the count(value) method. Its main purpose is to provide the frequency with which a certain value occurs in the specified sequence.

Index(value, start=0, end=None): Gives back the index of value’s first appearance. If the value cannot be located, a ValueError is raised.

clear(): Several Python data structures, such as lists, sets, dictionaries, deques, and maps, all use the clear() method. Its main purpose is to empty the collection by eliminating all of its components or key-value pairs.

Python programming requires an understanding of nested lists, built-in list functions, and list methods, particularly when working with data structures and algorithms. They offer strong tools for data organisation, manipulation, and query.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index