Lists in Python are versatile data structures that store ordered collections of elements. They're mutable, allowing easy modification, and can contain various data types. This flexibility makes lists essential for organizing and manipulating data in Python programs. Creating, accessing, and modifying lists are fundamental skills in Python. List methods like append(), remove(), and pop() enable dynamic list manipulation. Slicing, nested lists, and list comprehensions provide powerful ways to work with list data efficiently.
[]
with elements separated by commas[]
, with elements separated by commasmy_list = [1, 2, 3, 4, 5]
creates a list of integerslist()
constructor function
my_list = list()
creates an empty listlist()
to create a new list with the elements from the iterablemixed_list = [1, "apple", True, 3.14]
duplicate_list = [1, 2, 2, 3, 3, 3]
len()
function can be used to determine the number of elements in a list
length = len(my_list)
returns the length of my_list
my_list[0]
accesses the first element of my_list
my_list[-1]
accesses the last element of my_list
my_list[-2]
accesses the second-to-last element, and so onIndexError
in
operator can be used to check if an element exists in a list
if "apple" in my_list:
checks if the string "apple" is present in my_list
index()
method can be used to find the index of the first occurrence of an element in a list
my_list.index("apple")
returns the index of the first occurrence of "apple" in my_list
append(element)
: Adds an element to the end of the list
my_list.append(6)
adds the integer 6 to the end of my_list
extend(iterable)
: Extends the list by appending elements from an iterable
my_list.extend([7, 8, 9])
adds the elements 7, 8, and 9 to the end of my_list
insert(index, element)
: Inserts an element at a specified index
my_list.insert(2, "apple")
inserts the string "apple" at index 2 in my_list
remove(element)
: Removes the first occurrence of an element from the list
my_list.remove("apple")
removes the first occurrence of "apple" from my_list
pop(index)
: Removes and returns the element at a specified index
item = my_list.pop(1)
removes and returns the element at index 1 in my_list
clear()
: Removes all elements from the list
my_list.clear()
removes all elements, resulting in an empty listcount(element)
: Returns the number of occurrences of an element in the list
occurrences = my_list.count(2)
returns the count of the element 2 in my_list
list[start:end:step]
start
: The starting index of the slice (inclusive), default is 0 if not specifiedend
: The ending index of the slice (exclusive), default is the length of the list if not specifiedstep
: The step value or stride, default is 1 if not specifiedmy_list[1:4]
returns a new list containing elements from index 1 to 3 (exclusive) of my_list
my_list[:3]
returns a new list containing elements from the beginning up to index 2 (exclusive)my_list[2:]
returns a new list containing elements from index 2 to the end of my_list
my_list[::2]
returns a new list containing every other element of my_list
(step of 2)my_list[-3:]
returns a new list containing the last three elements of my_list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
creates a nested list with three inner listsnested_list[0][1]
accesses the element at index 1 of the first inner listnested_list[1].append(10)
appends the element 10 to the second inner listflat_list = [item for sublist in nested_list for item in sublist]
flattens nested_list
into flat_list
[expression for item in iterable if condition]
expression
: The element to be included in the new list, can involve computations or transformationsitem
: The variable representing each element in the iterableiterable
: The source iterable (list, tuple, string, etc.) to iterate overcondition
(optional): A boolean condition that filters the elements to be included in the new listsquared_list = [x**2 for x in range(1, 6)]
creates a new list with the squares of numbers from 1 to 5even_list = [x for x in my_list if x % 2 == 0]
creates a new list with only the even elements from my_list
product_list = [x*y for x in [1, 2, 3] for y in [4, 5, 6]]
creates a list of products of elements from two listsappend()
and pop()
sort()
method or the sorted()
function
my_list.sort()
sorts the elements of my_list
in ascending ordersorted_list = sorted(my_list, reverse=True)
creates a new list with the elements of my_list
sorted in descending orderfor
or while
) to iterate over elements and perform operations
for item in my_list: print(item)
iterates over each element in my_list
and prints itzip()
function to combine multiple lists element-wise
zipped_list = list(zip(list1, list2))
creates a new list of tuples combining elements from list1
and list2