In-Depth Guide To Python Lists!

In-Depth Guide To Python Lists!

In Progress Article!

What are lists in Python ?

  1. In Python, a list is a versatile and mutable ordered collection of elements. Lists are used to store and manipulate data efficiently. Each element in a list can be of any data type.

  2. Below is the follwing code to create a simple list in python3:

Ls = [1, 2.14, 3, 'Hello, Element!']

Slicing in Python ?

You can extract a portion of a list using slicing.

print(Ls[0])      # Output: 1
print(Ls[-1])     # Output: Hello, Element!

'''
    @brief: 
    1. We Can select N numbers of element the list as well.
    2. Where, 1 is inclusive, and 4 is exclusive.  
'''
print(Ls[1:4]]    # Output: [2.14, 3, 'Hello, Element!']

Mutability

  1. List are modifiable in nature.

Due, to which List mightn't have as good as performance as Immutable data-types.

Ls[0] = '1'
print(Ls)         # Output: ['1', 2.14, 3, 'Hello, Element!']

Iteration over a list

  1. First way is to iterate over elements directly:
friends = ['friend1', 'friend2', 'friend3']

for friend in friends:
    print(friend)
  1. Second way would be to iterate using C-Fashioned array style:
friends = ['friend1', 'friend2', 'friend3']

for n in range(len(friends)):
    print(friends[n])

Learning About List Methods

myList = [1, 2, 3]
myList.append(4)
"""
    @Syntax: list.append(x)
    @Parameters:
        x: The element to be added to the end of the list.
    @Function: Adds the specified element to the end of the list.
"""
print(myList)


myList.clear()
"""
    @Syntax: list.clear()
    @Removes all elements from the list.
"""
print(myList)

originalList = [1, 2, 3]
copiedList = originalList.copy()
"""
    @Syntax: list.copy()
    @Parameters:
        None
    @Function: Creates a shallow copy of the list.
"""
print(copiedList)


myList = [1, 2, 2, 3, 2, 4]
countOfTwos = myList.count(2)
"""
    @Syntax: list.count(x)
    @Parameters:
        x: The element to count.
    @Function: Returns the number of occurrences of the specified element in the list.
"""
print(countOfTwos)


list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
"""
    @Syntax: list.extend(iterable)
    @Parameters:
        iterable: The iterable whose elements will be added to the list.
    @Function: Adds elements from the iterable to the end of the list.
"""
print(list1)


myList = [10, 20, 30, 40, 50]
indexOf30 = myList.index(30)
"""
    @Syntax: list.index(x)
    @Parameters:
        x: The element to find in the list.
    @Function: Returns the index of the first occurrence of the specified element.
"""
print(indexOf30)

# insert(index, element): Inserts an element at the specified index in the list.
myList = [1, 2, 3]
myList.insert(1, 4)
"""
    @Syntax: list.insert(index, element)
    @Parameters:
        index: The index at which the element should be inserted.
        element: The element to be inserted.
    @Function: Inserts the element at the specified index in the list.
"""
print(myList)

myList = [1, 2, 3, 4]
poppedElement = myList.pop(2)
"""
    @Syntax: list.pop(index)
    @Parameters:
        index (int, optional): The index of the element to be removed.
    @Function: Removes and returns the element at the specified index.
    @Note: If the index is not specified, it removes and returns the last element of the list.
"""
print(poppedElement, myList)


myList = [10, 20, 30, 20, 40]
myList.remove(20)
"""
    @Syntax: list.remove(x)
    @Parameters:
        x: The element to be removed.
    @Removes the first occurrence of the specified element from the list.
"""
print(myList)


myList = [1, 2, 3, 4]
myList.reverse()
"""
    @Syntax: list.reverse()
    @Function: Reverses the order of elements in the list.
"""
print(myList)


myList = [3, 1, 4, 1, 5, 9, 2]
myList.sort()
"""
    @Syntax: list.sort(key=None, reverse=False)
    @Parameters:
        key (function, optional): A function to extract a comparison key from each element.
        reverse (bool, optional): If True, the list is sorted in descending order.
    @Function: Sorts the elements of the list in ascending order (default) or a custom order.
"""
print(myList)