Skip to content

Latest commit

 

History

History
249 lines (172 loc) · 7.85 KB

lists.md

File metadata and controls

249 lines (172 loc) · 7.85 KB

Lists

Definition and Initialization

In Python, a list is a collection of elements that can be of different types. Lists are defined with square brackets and elements are separated by commas. You can initialize a list at the time of declaration:

myList = [10, 20, 30, 40, 50] # A list of integers
anotherList = ['bananas', 'apples', 'oranges'] # A list of strings

Unlike C++, the size of a list in Python is dynamic and can change during runtime:

size = 10
myList = [0] * size # Creates a list with 10 elements, all initialized to 0

Lists with Multiple Types

A significant feature of Python lists is their ability to store different types of elements within the same list. This is quite different from arrays in languages like C++, where an array can only store elements of a single data type. Here's an example of a Python list containing both integers and strings:

mixed_list = [1, "Hello", 3.14, "World"]
print(mixed_list)

Accessing Elements

Python lists are ordered collections, meaning the items in a list appear in a specific order. This order is maintained, and each element in the list can be accessed by its position, or index. Indexes in Python lists start at 0 for the first element. Here's an example to illustrate this:

firstValue = myList[0] # Accessing the first element
myList[3] = 100 # Changing the value of the fourth element

Size

You can find the size (number of elements) of a list using the len() function:

size = len(myList) # Gets the number of elements in the list

List Methods

myList.append(60)  # Adds an element to the end
myList.insert(2, 25)  # Inserts 25 at index 2
myList.pop()  # Removes and returns the last element
myList.remove(20)  # Removes the first occurrence of 20
myList.sort()  # Sorts the list
myList.reverse()  # Reverses the list
myList.extend([70, 80])  # Extends myList by appending elements from the iterable
myList.clear()  # Removes all items from the list
myList.index(25)  # Returns the index of the first occurrence of 25
myList.count(10)  # Returns the number of times 10 appears in the list
myList.copy()  # Returns a shallow copy of the list

The list(...) Syntax

The list() function in Python is used to create a list. This can be useful in several scenarios, particularly when converting other iterable types (like tuples, sets, or strings) into a list. Here's how it differs from using square brackets []:

Conversion from Other Types: The list() function is explicitly used for converting other iterable objects to a list.

my_tuple = (1, 2, 3)
my_list = list(my_tuple)  # Converting a tuple to a list
print(my_list)  # Output: [1, 2, 3]

Creating a New List: While [] is used to create a new, empty list or a list with predefined elements, list() is typically used for conversion. However, list() can also be used to create an empty list.

new_list = []  # Creating an empty list using square brackets
another_new_list = list()  # Creating an empty list using list()

Readability: In cases where you are converting other iterables to lists, using list() can make the code more readable and explicit about the conversion happening.

Check it item(s) are in list

Python offers straightforward ways to check if an item, or multiple items, are present in a list. This is done using the in operator.

Checking Single Item

To check if a single item exists in the list:

if 20 in myList:
    print("20 is in the list")

if 100 not in myList:
    print("100 is not in the list")

Checking Multiple Items

To check if multiple items are in the list, you can use a loop or a comprehension:

sublist = [1, 2, 3]
mainlist = [2, 3, 4, 5, 6]

if all(elem in mainlist for elem in sublist):
    print("All elements of sublist are in mainlist")
else:
    print("Not all elements of sublist are in mainlist")

Concatenation and Repetition

Concatenation: You can concatenate two lists using the + operator.

combined_list = [1, 2, 3] + [4, 5, 6]  # Results in [1, 2, 3, 4, 5, 6]

Repetition: You can repeat lists with the * operator.

repeated_list = [1, 2, 3] * 2  # Results in [1, 2, 3, 1, 2, 3]

Slicing

Slicing in Python allows you to create sub-lists from an existing list. This is done using the slicing operator : within square brackets. The basic syntax for slicing is list[start:end:step], where start is the index where the slice starts, end is the index where the slice ends, and step is the step size or the increment between elements in the slice.

myList = [10, 20, 30, 40, 50, 60, 70, 80]
subList = myList[1:5]  # Elements from index 1 to 4
lastThree = myList[-3:]  # Last three elements of the list
alternate = myList[::2]  # Every second element
reverseList = myList[::-1] # reverseList will be [80, 70, 60, 50, 40, 30, 20, 10]
firstHalf = myList[:4]  # Elements from the beginning to index 3
secondHalf = myList[4:]  # Elements from index 4 to the end
# If the start or end index is out of range, Python handles it gracefully without throwing an error
extendedSlice = myList[2:100]  # From index 2 to an out of range index

Example of Using a List

Here's a simple example of declaring, initializing, and using a list in Python:

numbers = [10, 20, 30, 40, 50]

print("List elements are: ")
for number in numbers:
    print(number, end=" ")

Multidimensional Lists

Python supports lists of lists, which can be used as multidimensional arrays:

multiList = [[0 for _ in range(4)] for _ in range(3)] # A 3x4 list

Caveats and Considerations

  • Python lists are dynamic, they can grow or shrink in size, which is different from static arrays in C++.

  • Iterating over large lists can be slower than arrays in C++ due to Python's dynamic typing

  • Python lists can store elements of different types, including other lists, which is a significant difference from arrays in statically typed languages like C++.

  • Lists are managed dynamically in Python, providing more flexibility than static arrays in C++.

  • When passing lists to functions in Python, they are passed by reference, meaning the function can modify the list.

    def print_list(lst):
        for element in lst:
            print(element)
    
    print_list(numbers)

Creating a Readonly Dictionary

In Python, there isn't a direct equivalent to C#'s IReadOnlyList, but you can achieve similar functionality with a few approaches:

Using tuple

A simple and straightforward way to have an immutable list-like structure is to use a tuple. Tuples are immutable in Python and can be used similarly to lists for most read operations.

immutable_list = (1, 2, 3)
# immutable_list[0] = 4  # This will raise a TypeError

Subclassing list and Overriding Mutating Methods

You can create a subclass of list and override methods that mutate the list.

class ImmutableList(list):
    def __setitem__(self, index, value):
        raise TypeError("Cannot modify ImmutableList")

    def __delitem__(self, key):
        raise TypeError("Cannot modify ImmutableList")

    # Other mutating methods like append, extend, etc. can be overridden similarly.

# Usage
immutable_list = ImmutableList([1, 2, 3])
# immutable_list[0] = 4  # Raises TypeError

Custom Wrapper Class

Creating a custom wrapper class that only exposes read methods of the list can provide a read-only view.

class ReadOnlyList:
    def __init__(self, data):
        self._data = list(data)

    def __getitem__(self, key):
        return self._data[key]

    def __len__(self):
        return len(self._data)

    def __iter__(self):
        return iter(self._data)

    # You can add other list read methods here

# Usage
original_list = [1, 2, 3]
read_only_list = ReadOnlyList(original_list)