Python
Basic Concepts
Pop() del
• Used with lists and dictionaries. • General-purpose deletion
• Removes an element and returns statement.
it. • Does not return anything.
• For lists → pop(index) removes the • Can delete variables, list
element at a given index (default is slices, items in lists, keys in
the last element). dictionaries, or even entire
• For dicts → pop(key) removes the objects.
key–value pair and returns the
value.
del
• Delete a variable
x = 10
del x
• Delete an item from a list
numbers = [1, 2, 3, 4, 5]
del numbers[2] # deletes element at index 2 → 3
print(numbers) # Output: [1, 2, 4, 5]
del
• Delete a slice of a list (multiple elements at once)
nums = [10, 20, 30, 40, 50, 60]
del nums[1:4] # deletes elements at index 1,2,3 → 20,30,40
print(nums) # Output: [10, 50, 60]
• Delete entire list
my_list = [10, 20, 30, 40]
del my_list # deletes the entire list
Now trying to access it will give an error:
print(my_list) → NameError: name 'my_list' is not defined
del
• Delete a key–value pair in a dictionary
data = {'a': 1, 'b': 2, 'c': 3}
del data['b'] # removes key 'b' and its value
print(data) # {'a': 1, 'c': 3}
sort()
• It works on Lists only
nums = [5, 2, 9, 1]
nums.sort()
print(nums) # [1, 2, 5, 9]
• It arranges the list in-place (i.e., it changes the original list) in
ascending order by default.
nums = [5, 2, 9, 1]
nums.sort()
print(nums) # [1, 2, 5, 9]
print(nums[2]) # 5
sort()
• It does not return a new list → it returns None.
sort() : parameters
• reverse
nums = [5, 2, 9, 1]
nums.sort(reverse=True)
print(nums) # [9, 5, 2, 1]
• nums.sort(reverse=False)
• Key
words = ['apple', 'kiwi', 'banana', 'fig']
words.sort(key=len)
print(words) # Output: ['fig', 'kiwi', 'apple', 'banana']
sorted()
• If need a new sorted list without changing the original, use sorted()
instead of using sort()
nums = [5, 2, 9, 1]
new_list = sorted(nums)
print(new_list) # [1, 2, 5, 9]
print(nums) # [5,2,9,1] (original list remains unchanged)
sort() vs sorted()
Method Behavior Returns new list?
sort() Sorts the same list No
sorted() Creates a new sorted list Yes
Exercise
• Create a list that contains numeric and strings values then use
sort() method.
Exercise
• Create a list that contains strings with lowercase and upper case
then use sort() method
how to print last element of a list
• my_list = [10, 20, 30, 40, 50]
• print(my_list[-1])
copy()
• List1 = [23, 24, ’element’, ’apple’]
• List2= List1
• print(List1) # ➝ [23, 24, 'element', 'apple’]
• print(List2) # ➝ [23, 24, 'element', 'apple’]
• Proof
• List1.append('banana’)
or
• List2.append('banana')
• print(List1) # ➝ [23, 24, 'element', 'apple', 'banana']
• print(List2) # ➝ [23, 24, 'element', 'apple', 'banana']
Explanation
• Its not copy the list rather only provide the reference of the list.
• Both List1 and List2 refer to the same list object in memory, not
two separate copies.
• Changes in one list will reflect in other list.
copy()
• copy() is a list method in Python used to make a new copy of a list,
meaning it creates a new list object with the same elements,
instead of just another reference to the original list.
List1 = [23, 24, 'element', 'apple’]
List2= List1.copy()
print(List2)
Output : [23, 24, 'element', 'apple’]
Proof
Another method: list() contructor
• The list() constructor is a built-in Python function that creates a
new list object.
• Convert other data types (like tuples, strings, sets) into a list.
• Or make a copy of an existing list.
• Syntax:
• List2 = list(List1)
Error?
list() creates new list
in
• The in operator in Python is used to check whether a value exists
inside a sequence or collection( list, string, tuple, set, dictionary)
• It returns a Boolean value:
• True if the item is present
• False if it’s not present
Eg: with a list
• List1 = [23, 24, 'element', 'apple’]
• print(23 in List1) # True
• print('apple' in List1) # True
• print(100 in List1) # False
• print('banana' in List1) # False
Eg: with a string
Eg: With Loops
It tells Python to take each item in the sequence one by one.
Eg: with if
if 'apple' in List1: checks if 'apple' exists inside the list List1.
If present, the code inside the if block runs.
Otherwise, the else block runs
Eg: with if
Eg: with if (numeric)
Error?
DIY
• count()
• Index()