1. Introduction to Lists in Python
In Python, a list can be defined as a collection of values or items. The items in the list are separated with a comma (,) and enclosed within square brackets [].
L1 = []
print(L1)
L2 = [123, "python", 3.7]
print(L2)
L3 = [1, 2, 3, 4, 5, 6]
print(L3)
L4 = ["C", "Java", "Python"]
print(L4)
2. List Indexing
Like string sequences, the indexing of Python lists starts from 0. The first element of the list is stored at the 0th index. Elements can be accessed using the slice operator [].
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[0])
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[2])
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[1:3])
Negative Indexing
Unlike other languages, Python provides the flexibility to use negative indexing. The negative indices are counted from the right.
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-1])
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-3])
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-4:-2])
3. List Operators
num = [1, 2, 3, 4, 5]
lang = ['python', 'c', 'java', 'php']
print(num + lang)
num = [1, 2, 3, 4, 5]
print(num * 2)
lang = ['python', 'c', 'java', 'php']
print('cpp' in lang)
num = [1, 2, 3, 4, 5]
print(6 not in num)
🧠Pop Quiz 1: Creation & Indexing
Question: If fruits = ["apple", "banana", "cherry", "date"], what will print(fruits[-3:-1]) output?
Click here to reveal the answer
Answer: ['banana', 'cherry']
Explanation: Negative indexing counts from the right. "date" is -1, "cherry" is -2, "banana" is -3. The slice goes from -3 up to (but not including) -1.
4. Updating and Deleting Elements
Updating Elements
Python allows us to modify the list items by using the assignment operator (=).
num = [1, 2, 3, 4, 5]
num[2] = 30
print(num)
num = [1, 2, 30, 4, 5]
num[1:3] = [25, 36]
print(num)
Deleting Elements
Python allows us to delete one or more items in a list by using the del keyword.
num = [1, 2, 3, 4, 5]
del num[1]
print(num)
num = [1, 3, 4, 5]
del num[1:3]
print(num)
5. Iterating a List
A list can be iterated by using a for in loop.
lang = ['python', 'c', 'java', 'php']
print("The list items are \n")
for i in lang:
print(i)
🧠Pop Quiz 2: Modification
Question: How would you permanently remove the first element of a list named data?
Click here to reveal the answer
Answer: del data[0]
6. List Functions & Methods
num = [1, 2, 3, 4, 5, 6]
print("length of list :", len(num))
list1 = [1, 2, 3, 4, 5, 6]
print("Max of list1 :", max(list1))
list2 = ['java', 'c', 'python', 'cpp']
print("Min of list2 :", min(list2))
list1 = [1, 2, 3, 4, 5, 6]
print("Sum of list items :", sum(list1))
num = [1, 3, 2, 4, 6, 5]
print(sorted(num))
list1 = [6, 8, 2, 4, 10]
list1.sort(reverse=True)
print(list1)
num = [1, 2, 3, 4, 5]
num.append(6)
print(num)
num = [10, 20, 30, 40, 50]
num.insert(4, 60)
print(num)
list1 = [1, 2, 3, 4, 5]
list1.remove(2)
print(list1)
num = [10, 20, 30, 40, 50]
num.pop()
print(num)
num = [10, 20, 30, 40, 50]
num.clear()
print(num)
num = [1, 2, 3, 4, 3, 2, 2, 1, 4, 5, 8]
cnt = num.count(2)
print("Count of 2 is:", cnt)
list1 = ['p', 'y', 't', 'o', 'n', 'p']
print(list1.index('t'))
list1 = [6, 8, 2, 4, 10]
list1.reverse()
print(list1)
🧠Pop Quiz 3: Functions and Methods
Question: What is the primary difference between remove() and pop()?
Click here to reveal the answer
Answer: remove() deletes an element by specifying its value (e.g., remove("apple")) and removes the first occurrence. pop() deletes an element by specifying its index (e.g., pop(1)) and returns the removed element.
0 Comments