CONTENTS
Introduction
Python list is highly versatile data structure that allows you to store an ordered collection of heterogenous items.
Think of it as a container that can hold various types of data, including numbers, strings, and even other lists. What makes lists particularly powerful is their mutability, meaning you can modify them after creation by adding, removing, or changing elements.
These ordered sequences are defined by enclosing comma-separated items within square brackets, and each item within a list is assigned an index, beginning with zero, which enables you to access and manipulate individual elements efficiently.
my_list = [ 1, 2, 1.2, 'ybi foundation, True]
Creating List
Creating lists in Python is a straightforward process. The most common way is to enclose a comma-separated sequence of items within square brackets [ ]. This allows you to directly initialize a list with specific values, whether they are numbers, strings, or a mix of data types.
For instance, my_list = [1, "hello", 3.14] creates a list containing an integer, a string, and a float.
Alternatively, you can create an empty list using my_list = [ ] and then add elements later using methods like append() or insert().
Another powerful technique is to use the list() constructor, which can convert other iterable data types, such as tuples or strings, into lists.
List Indexing
List Methods
In Python, lists are one of the most versatile and commonly used data structures. They come with a variety of built-in methods and functions that allow you to manipulate and interact with the data they contain. Below is a list of commonly used list methods and functions:
1. append(item): Adds an item to the end of the list.
Output:
1. append(item): Adds an item to the end of the list.
Output:
2. extend(iterable): Adds all elements of an iterable (e.g., list, tuple, string) to the end of the list.
Code </>:
# adds 1, 2, 3 to the end of my_list
my_list.extend([1, 2, 3])
Output:
3. insert(index, item): Inserts an item at a specified index.
Code </>:
# inserts 'x' at index 1
my_list.insert(1, 'x')
Output:
4. remove(item): Removes the first occurrence of the specified item.
Code </>:
# removes the first 'x' from my_list
my_list.remove('x')
Output:
5. pop([index]): Removes and returns the item at the specified index. If no index is specified, it removes and returns the last item.
Code </>:
# removes and returns the item at index 1
my_list.pop(1)
Output:
6. clear(): Removes all items from the list.
Code </>:
# empties the list
my_list.clear()
Output:
7. index(item[, start[, end]]): Returns the index of the first occurrence of the specified item. You can specify a start and end index to search within a sublist.
Code </>:
# returns the index of the first 'x'
my_list.index('x')
Output:
8. count(item): Returns the number of times the specified item appears in the list.
Code </>:
# returns the count of 'x' in my_list
Output:
9. sort(key=None, reverse="False): Sorts the list in place. You can specify a key function for custom sorting and a reverse flag to sort in descending order.
Code </>:
# sorts the list in descending order
Output:
10. reverse(): Reverses the elements of the list in place.
Code </>:
# reverses the order of elements in my_list
Output:
11. copy(): Returns a shallow copy of the list.
Code </>:
# creates a copy of my_list
Output:
List Functions
1. len(list): Returns the number of items in the list.
Code </>:
# returns the length of my_list
Output:
2. min(list): Returns the smallest item in the list.
Code </>:
# returns the smallest element in my_list
Output:
3. max(list): Returns the largest item in the list.
Code </>:
# returns the largest element in my_list
Output:
4. sum(list): Returns the sum of all items in the list (only for numeric lists).
Code </>:
# returns the sum of all elements in my_list
Output:
5. sorted(list, key=None, reverse= False): Returns a new sorted list from the elements of the list. The original list remains unchanged.
Code </>:
# returns a new list sorted in descending order
Output:
6. any(list): Returns True if at least one element in the list is True (or truthy).
Code </>:
# returns True if any element in my_list is True
Output:
7. all(list): Returns True if all elements in the list are True (or truthy).
Code </>:
# returns True if all elements in my_list are True
Output:
8. list(iterable): Converts an iterable (e.g., tuple, string, set) into a list.
Code </>:
# converts the string 'hello' into ['h', 'e', 'l', 'l', 'o']
Output:
