Advanced Python - I
Introduction to Lists
Simply put, a list is an ordered collection of items enclosed in [] and separated by commas. Lists in Python work like dynamic arrays, similar to vector in C++ or ArrayList in Java.
Lists belong to Python’s sequence data types, just like tuples and strings.
Example of a List in Python
Let’s create a Python list:
Output:
Lists are the simplest containers and are fundamental in Python programming. They do not need to be homogeneous, which makes them versatile. A list can store integers, strings, objects, or mixed data types. Lists are mutable, meaning they can be modified after creation.
Creating a List in Python
Lists can be created by placing a series of items inside square brackets []. Unlike Sets, lists do not need a constructor function for creation.
👀Note: Unlike Sets, lists can hold mutable elements like other lists.
Examples:
Creating a Blank List
Creating a List of Numbers
Creating a List of Strings and Accessing Elements
Output:
Accessing Elements from the List
List items are accessed by their index using square brackets []. Nested lists can be accessed with multiple indices.
Example 1: Accessing Elements from a List
Output:
Example 2: Accessing Elements from a Multi-Dimensional List
Output:
Negative Indexing
Negative indices access elements from the end of the list. For example, list[-1] retrieves the last item, list[-2] the second-last, etc.
Output:
Getting the Size of a List
Use the len() function to get the number of items in a list.
Output:
Adding Elements to a Python List
append() Method
Use append() to add a single element to the end of a list. To add multiple items, loop through your data.
Output:
insert() Method
The insert() method adds an element at a specified index.
Syntax:
-
index: The position where to insert.
-
element: The element to insert.
Example 1: Inserting a String
Output:
Example 2: Inserting an Integer into a List
Output:
count() Method
The count() method counts how many times a value appears in a list.
Syntax:
Example:
Output:
del Keyword
The del keyword removes items by index or deletes the entire list.
Syntax:
Example:
Output:
remove() Method
The remove() method deletes the first occurrence of a specified value.
Syntax:
Example:
Output:
pop() Method
The pop() method removes and returns an item at a specific index. If no index is given, it pops the last item.
Syntax:
Example:
Output:
max() Method
The max() function returns the largest element among the values given. For strings, it returns the lexicographically highest value.
Syntax:
Example:
Output:
min() Method
The min() function returns the smallest element among the arguments. For strings, it returns the lexicographically smallest string.
Syntax:
Example:
Output:
sort() Method
The sort() method arranges list elements in ascending or descending order.
Syntax:
-
reverse:
Truefor descending order. key: A function to decide sorting logic.
Example 1: Sort Numbers in Ascending Order
Output:
reverse() Method
The reverse() method reverses the list in place.
Syntax:
Example:
Output: