Python guide on using [‘LISTS’]
A `LIST` is a built-in data structure that represents an ordered collection of elements. It is one of the most commonly used data structures and provides flexibility for storing and manipulating data.
Creating a List: You can create a list in Python by enclosing comma-separated values in square brackets
#Creating a List
l = [1, 2, 3, 4, 5]Accessing List Elements: You can access individual elements of a list using indexing. Python uses zero-based indexing, so the first element is at index 0. You can also use negative indexing, beginning from the last element being at -1.
#Accessing List Elements
print(l[0])
# Output: 1
print(l[2])
# Output: 3Modifying List Elements: Lists are mutable, meaning you can modify individual elements.
#Modifying List Elements
l[1] = 10
print(l)
# Output: [1, 10, 3, 4, 5]List Length: You can find the length of a list using the len() function.
#List Length
len_l = len(l)
print(len_l)
# Output: 5Adding Elements to a List: You can add elements to a list using the append() method.
#Adding Elements to a List
l.append(6)
print(l)
# Output: [1, 10, 3, 4, 5, 6]Removing Elements from a List: You can remove elements from a list using the remove() method or the del statement.
#Removing Elements from a List
l.remove(3)
print(my_list)
# Output: [1, 10, 4, 5, 6]
del l[0]
print(l)
# Output: [10, 4, 5, 6]Slicing Lists: You can extract a portion of a list using slicing. Slicing allows you to specify a range of indices to include.
#Slicing Lists
slic_list = l[1:3]
print(slic_list)
# Output: [10, 4]List Concatenation: You can concatenate two or more lists using the + operator.
#List Concatenation
l1 = [1, 2, 3]
l2 = [4, 5, 6]
con_list = l1 + l2
print(con_list)
# Output: [1, 2, 3, 4, 5, 6]Inserting Elements: You can insert an element at a specific index in a list using the insert() method.
#Inserting Elements
l.insert(2, 7)
# Inserts 7 at index 2
print(l)
# Output: [1, 10, 7, 4, 5, 6]Extending a List: You can extend a list by appending all elements from another iterable using the extend() method.
#Extending a List
l3 = [8, 9]
l.extend(l3)
print(l)
# Output: [1, 10, 7, 4, 5, 6, 8, 9]Counting Occurrence: You can count the number of occurrences of an element in a list using the count() method.
#Counting Occurance of Elements
c = l.count(4)
print(c)
# Output: 1Reversing a List: You can reverse the order of elements in a list using the reverse() method.
#Reversing a List
l.reverse()
print(l)
# Output: [9, 8, 6, 5, 4, 7, 10, 1]Finding the Index: if you want to find the index of an element in a list, you can use the .index() method.
#Finding the Index
l1 = [10, 12, 23, 34, 45]
ind_l = l.index(12)
print(ind_l)
# Output: 1Copying a List: You can create a shallow copy of a list using the copy() method or by using the slice notation [:]
#Copying a List
new_l = l.copy()
print(new_l)
new_l1 = l[:]
print(new_l1)Removing and Returning Elements: You can remove and return an element from a list using the pop() method. If no index is provided, it removes and returns the last element.
#Removing and Returning Elements
ele = l.pop()
print(ele)
# Output: 1
print(l)
# Output: [9, 8, 6, 5, 4, 7, 10]List Comprehension: List comprehension provides a concise way to create new lists based on existing lists.
#List Comprehension
num_l = [1, 2, 3, 4, 5]
sqr_l = [x**2 for x in numbers]
print(sqr_l)
# Output: [1, 4, 9, 16, 25]Deleting an Element by Index: You can use del to remove an element from a list by specifying its index.
#Deleting an Element by Index
l = [1, 2, 3, 4, 5]
del l[2] # Deletes the element at index 2
print(my_list)
# Output: [1, 2, 4, 5]
#also refer to remove()
#------------------------
#You can also delete a range of elements from a list using slicing
l = [1, 2, 3, 4, 5]
del l[1:3] # Deletes elements at index 1 and 2
print(l)
# Output: [1, 4, 5]
#------------------------
#If you want to delete the entire list and free up memory,
#you can use del without specifying an index
l = [1, 2, 3, 4, 5]
del l
# The list 'my_list' is deleted and no longer accessibleLists are versatile and can be used in a wide range of programming tasks, from simple data storage to complex data manipulation and algorithm implementation.
Get Dr. Pooja’s stories in your inbox
Join Medium for free to get updates from this writer.
For more information on lists please refer to the documentation .
Keep Learning!





