Lists in Python
A list in Python is a built-in data structure that allows you to store multiple items in a
single variable. Lists are ordered, mutable, and can contain heterogeneous elements (i.e.,
different data types like integers, strings, or even other lists).
Use Cases of Lists include:
Storing sequences of data (e.g., scores, names)
Implementing stacks, queues, or matrices
Managing dynamic collections in applications
example_list = [10, "hello", 3.14, [1, 2]]
Key Characteristics
Ordered: Items have a defined order and can be accessed via index.
Mutable: You can change, add, or remove elements after the list is created.
Dynamic: Lists can grow or shrink as needed.
Indexed: Indexing starts from 0.
Creating a List
A list can be created using square brackets [] and separating elements with commas.
list1 = [10, 20, 30, 40]
List Operations
Each operations demonstrates how flexible and powerful lists are for managing
collections of data.
Operation Syntax / Method Description
Append list.append(item) Adds an item to the end of the list
Insert list.insert(index, item) Inserts an item at a specific index
Remove list.remove(item) Removes the first occurrence of the item
Pop list.pop(index) Removes and returns item at index (default last)
Indexing list[index] Accesses item at a specific index
Slicing list[start:end] Returns a sublist from start to end-1
Sorting list.sort() Sorts the list in ascending order
Length len(list) Returns the number of elements
Example:
print("\t\t\t------")
print("\t\t\tLISTS")
print("\t\t\t------")
print("1.Creating a list")
print("---------------")
list1=[10,20,60,40,30]
print("Original List : ",list1)
print("")
print("2.Appending a list")
print("------------------")
list1.append(50)
print("After appending : ",list1)
print("")
print("3.Inserting into a list")
print("------------------------")
list1.insert(2,45)
print("After inserting at index 2 : ",list1)
print("")
print("4.Removing an element")
print("---------------------")
list1.remove(45)
print("After removing 45: ",list1)
print("")
print("5.Retrieving an element by index")
print("--------------------------------")
print("Element at 4 : ",list1[4])
print("")
print("6.Slicing a list")
print("----------------")
print("After slicing : ",list1[2:5])
print("")
print("7.Sorting a list")
print("----------------")
list1.sort()
print("After sorting : ",list1)
print("")
Output:
------
LISTS
------
1.Creating a list
---------------
Original List : [10, 20, 60, 40, 30]
2.Appending a list
------------------
After appending : [10, 20, 60, 40, 30, 50]
3.Inserting into a list
------------------------
After inserting at index 2 : [10, 20, 45, 60, 40, 30, 50]
4.Removing an element
---------------------
After removing 45: [10, 20, 60, 40, 30, 50]
5.Retrieving an element by index
--------------------------------
Element at 4 : 30
6.Slicing a list
----------------
After slicing : [60, 40, 30]
7.Sorting a list
----------------
After sorting : [10, 20, 30, 40, 50, 60]
Nested Lists
Lists can contain other lists, allowing for multi-dimensional data structures.
nested = [[1, 2], [3, 4]]
print(nested[1][0]) # Output: 3
Looping Through Lists
for item in list1:
print(item)
This is useful for processing or displaying each element.