Day 6 of Learning Python for Data Science: Mastering Lists, Sets, and Logical Thinking
Data structures are the backbone of programming, enabling efficient organization and manipulation of data. Python provides several built-in data structures, such as:
In practical applications:
A list is a mutable, ordered collection that can store multiple data types.
Creating a list
students = ["Alice", "Bob", "Charlie"]
print(students) # Output: ['Alice', 'Bob', 'Charlie']
Python offers several methods for manipulating lists:
Adding elements
students.append("David") # Appends 'David' at the end
students.insert(1, "Eve") # Inserts 'Eve' at index 1
Removing elements
students.remove("Bob") # Removes 'Bob'
students.pop() # Removes the last element
Slicing lists
print(students[1:3]) # Outputs elements from index 1 to 2
Modifying elements
name = ['Vishal', 'Deepanjan', 'Satyam']
name[0] = 'Jaiswal'
print(name)
['Jaiswal', 'Deepanjan', 'Satyam']
append()
– Adds an element at the end of the list.
students = ["Alice", "Bob"]
students.append("Charlie")
print(students) # Output: ['Alice', 'Bob', 'Charlie']
insert()
– Inserts an element at a specific index.
students.insert(1, "David")
print(students) # Output: ['Alice', 'David', 'Bob', 'Charlie']
remove()
– Removes a specific element from the list.
students.remove("Bob")
print(students) # Output: ['Alice', 'David', 'Charlie']
pop()
– Removes and returns the last element.
last_student = students.pop()
print(last_student) # Output: 'Charlie'
print(students) # Output: ['Alice', 'David']
Slicing (list[start:end]
) – Extracts a portion of the list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
len()
– Returns the number of elements in the list.
print(len(numbers)) # Output: 5
sort()
– Sorts the list in ascending order.
numbers.sort()
print(numbers) # Output: [10, 20, 30, 40, 50]
reverse()
– Reverses the elements of the list.
numbers.reverse()
print(numbers) # Output: [50, 40, 30, 20, 10]
index()
– Returns the index of the first occurrence of an element.
print(numbers.index(30)) # Output: 2
A set is an unordered, mutable collection that stores only unique elements.
Creating a set
unique_numbers = {1, 2, 3, 3, 4, 5}
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
Python supports various set operations for mathematical and logical manipulations:
Common Set Methods:
union()
– Combines two sets, removing duplicates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
intersection()
– Returns common elements between two sets.
print(set1.intersection(set2)) # Output: {3}
difference()
– Returns elements present in the first set but not in the second.
print(set1.difference(set2)) # Output: {1, 2}
len()
– Returns the number of elements in a set.
print(len(set1)) # Output: 3
add()
– Adds a single element to the set.
set1.add(6)
print(set1) # Output: {1, 2, 3, 6}
update()
– Adds multiple elements to the set.
set1.update([7, 8, 9])
print(set1) # Output: {1, 2, 3, 6, 7, 8, 9}
discard()
– Removes an element from the set without error if it doesn’t exist.
set1.discard(2)
print(set1) # Output: {1, 3, 6, 7, 8, 9}
remove()
– Removes an element but raises an error if it doesn’t exist.
set1.remove(3)
print(set1) # Output: {1, 6, 7, 8, 9}
pop()
– Removes and returns an arbitrary element from the set.
print(set1.pop()) # Output: (random element from the set)
clear()
– Removes all elements from the set.
set1.clear()
print(set1) # Output: set()
issubset()
– Checks if all elements of a set exist in another set.
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2)) # Output: True
issuperset()
– Checks if a set contains all elements of another set.
print(set2.issuperset(set1)) # Output: True
isdisjoint()
– Returns True if two sets have no common elements.
set3 = {6, 7, 8}
print(set1.isdisjoint(set3)) # Output: True
copy()
– Returns a shallow copy of the set.
set_copy = set1.copy()
print(set_copy) # Output: {1, 2, 3}
[10, 20, 4, 45, 99]
→ Output: 45
k
, shift elements to the right by k
positions. Example: ([1, 2, 3, 4, 5], k=2)
→ Output: [4, 5, 1, 2, 3]
[1, 2, 2, 3, 4, 4, 5]
→ Output: [1, 2, 3, 4, 5]
([1, 2, 3, 4, 5, 6], target=7)
→ Output: [(1, 6), (2, 5), (3, 4)]
([10, 20, 30, 40], target=30)
→ Output: 2
{1, 2, 3, 4}
and {3, 4, 5, 6}
→ Output: {1, 2, 5, 6}
{1, 2}
is a subset of {1, 2, 3, 4}
→ Output: True
[{1, 2, 3}, {3, 4, 5}, {5, 6, 7}]
→ Output: 7
{1, 2, 3}
, {2, 3, 4}
, {3, 4, 5}
→ Output: {3}
set2
from set1
without using a loop. Example: {1, 2, 3, 4, 5}
minus {3, 4}
→ Output: {1, 2, 5}
Click here for solutions – Python Practice Questions & Solutions
On Day 6 of Python Learning, we explored:
Mastering these concepts will significantly enhance your data-handling skills in Python, paving the way for advanced topics in data science and algorithm design.
We hope this article was helpful for you and you learned a lot about data analyst interview from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Test your understanding of Python Data Structure, which we learned in our previous lesson of…
Introduction Welcome to Day 7 of Learning Python for Data Science. Today we will see…
Welcome back to Day 6 of Learning Python for Data Science journey! In the last…
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…