PyData Careers
20.7K subscribers
196 photos
4 videos
26 files
342 links
Python Data Science jobs, interview tips, and career insights for aspiring professionals.
Download Telegram
#Python #InterviewQuestion #DataProcessing #FileHandling #Programming #IntermediateLevel

Question: How can you efficiently process large CSV files in Python without loading the entire file into memory, and what are the best practices for handling such scenarios?

Answer:

To process large CSV files efficiently in Python without loading the entire file into memory, you can use generators or stream the data line by line. This approach is especially useful when working with files that exceed available RAM.

Here’s a detailed example using csv module and generator patterns:

import csv
from typing import Dict, Generator

def read_csv_large_file(file_path: str) -> Generator[Dict, None, None]:
"""
Generator function to read a large CSV file line by line.
Yields one row at a time as a dictionary.
"""
with open(file_path, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
yield row

def process_large_csv(file_path: str, threshold: int):
"""
Process a large CSV file, filtering rows based on a condition.
Example: Only process rows where 'age' > threshold.
"""
total_processed = 0
valid_rows = []

for row in read_csv_large_file(file_path):
try:
age = int(row['age'])
if age > threshold:
valid_rows.append(row)
total_processed += 1
# Optional: process row immediately instead of storing
# print(f"Processing: {row}")
except (ValueError, KeyError):
continue # Skip invalid or missing age fields

print(f"Total valid rows processed: {total_processed}")
return valid_rows

# Example usage
if __name__ == "__main__":
file_path = 'large_data.csv'
result = process_large_csv(file_path, threshold=30)
print("Processing complete.")

### Explanation:
- **csv.DictReader**: Reads each line of the CSV as a dictionary, allowing access by column name.
- **Generator (read_csv_large_file)**: Yields one row at a time, avoiding memory overMemory Efficiencyciency**: No need to load all data into memory; only one row is held at a Error Handlingndling**: Skips malformed or missing data gracefScalabilitybility**: Suitable for gigabyte-sized files.

This technique is essential in data engineering and analytics roles, where performance and memory efficiency are critical.

By: @DataScienceQ 🚀
#Python #InterviewQuestion #Concurrency #Threading #Multithreading #Programming #IntermediateLevel

Question: How can you use threading in Python to speed up I/O-bound tasks, such as fetching data from multiple URLs simultaneously, and what are the key considerations when using threads?

Answer:

To speed up I/O-bound tasks like fetching data from multiple URLs, you can use Python's threading module to perform concurrent operations. This is effective because threads can wait for I/O (like network requests) without blocking the entire program.

Here’s a detailed example using threading and requests:

import threading
import requests
from time import time

# List of URLs to fetch
urls = [
'https://httpbin.org/json',
'https://api.github.com/users/octocat',
'https://jsonplaceholder.typicode.com/posts/1',
'https://www.google.com',
]

# Shared list to store results
results = []
lock = threading.Lock() # To safely append to shared list

def fetch_url(url: str):
"""Fetches a URL and stores the response text."""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
with lock:
results.append({
'url': url,
'status': response.status_code,
'length': len(response.text)
})
except Exception as e:
with lock:
results.append({
'url': url,
'status': 'Error',
'error': str(e)
})

def fetch_urls_concurrently():
"""Fetches all URLs using multiple threads."""
start_time = time()

# Create a thread for each URL
threads = []
for url in urls:
thread = threading.Thread(target=fetch_url, args=(url,))
threads.append(thread)
thread.start()

# Wait for all threads to complete
for thread in threads:
thread.join()

end_time = time()
print(f"Time taken: {end_time - start_time:.2f} seconds")
print("Results:")
for result in results:
print(result)

if __name__ == "__main__":
fetch_urls_concurrently()

### Explanation:
- **threading.Thread**: Creates a new thread for each URL.
- **target**: The function to run in the thread (fetch_url).
- **args**: Arguments passed to the target start() **start()**: Begins execution of thjoin()- **join()**: Waits for the thread to finish before coLock.
- **Lock**: Ensures safe access to shared resources (like results) to avoid race conditions.

### Key ConsidGIL (Global Interpreter Lock)eter Lock)**: Python’s GIL limits true parallelism for CPU-bound tasks, but threads work well for I/O-bouThread Safetyead Safety**: Use locks or queues when sharing data betweenOverhead**Overhead**: Creating too many threads can degrade perTimeouts**Timeouts**: Always set timeouts to avoid hanging on slow responses.

This pattern is commonly used in web scraping, API clients, and backend services handling multiple external calls efficiently.

By: @DataScienceQ 🚀
1
#Python #InterviewQuestion #DataStructures #Algorithm #Programming #CodingChallenge

Question:
How does Python handle memory management, and can you demonstrate the difference between list and array in terms of memory efficiency with a practical example?

Answer:

Python uses automatic memory management through a private heap space managed by the Python memory manager. It employs reference counting and a garbage collector to reclaim memory when objects are no longer referenced. However, the way different data structures store data impacts memory efficiency.

For example, a list in Python stores pointers to objects, which adds overhead due to dynamic resizing and object indirection. In contrast, an array from the array module stores primitive values directly, reducing memory usage for homogeneous data.

Here’s a practical example comparing memory usage between a list and an array:

import array
import sys

# Create a list of integers
my_list = [i for i in range(1000)]
print(f"List size: {sys.getsizeof(my_list)} bytes")

# Create an array of integers (type 'i' for signed int)
my_array = array.array('i', range(1000))
print(f"Array size: {sys.getsizeof(my_array)} bytes")

Output:
List size: 9088 bytes
Array size: 4032 bytes

Explanation:
- The list uses more memory because each element is a Python object (e.g., int), and the list stores references to these objects. Additionally, the list has internal overhead for resizing.
- The array stores raw integer values directly in a contiguous block of memory, avoiding object overhead and resulting in much lower memory usage.

This makes array more efficient for large datasets of homogeneous numeric types, while list offers flexibility at the cost of higher memory consumption.

By: @DataScienceQ 🚀
1
#Python #InterviewQuestion #OOP #Inheritance #Polymorphism #Programming #CodingExample

Question:
How does method overriding work in Python, and can you demonstrate it using a real-world example involving a base class Animal and derived classes Dog and Cat?

Answer:

Method overriding in Python allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables polymorphism, where objects of different classes can be treated as instances of the same class through a common interface.

Here’s an example demonstrating method overriding with Animal, Dog, and Cat:

class Animal:
def make_sound(self):
pass # Abstract method

class Dog(Animal):
def make_sound(self):
return "Woof!"

class Cat(Animal):
def make_sound(self):
return "Meow!"

# Function to demonstrate polymorphism
def animal_sound(animal):
print(animal.make_sound())

# Create instances
dog = Dog()
cat = Cat()

# Call the method
animal_sound(dog) # Output: Woof!
animal_sound(cat) # Output: Meow!

Explanation:
- The Animal class defines an abstract make_sound() method.
- Both Dog and Cat inherit from Animal and override make_sound() with their own implementations.
- The animal_sound() function accepts any object that has a make_sound() method, showcasing polymorphism.
- When called with a Dog or Cat instance, the appropriate overridden method is executed based on the object type.

This demonstrates how method overriding supports flexible and extensible code design in object-oriented programming.

By: @DataScienceQ 🚀
3
#Python #InterviewQuestion #OOP #Inheritance #Polymorphism #Programming #CodingChallenge

Question:
How does method resolution order (MRO) work in Python when multiple inheritance is involved, and can you provide a code example to demonstrate the diamond problem and how Python resolves it using C3 linearization?

Answer:

In Python, method resolution order (MRO) determines the sequence in which base classes are searched when executing a method. When multiple inheritance is used, especially in cases like the "diamond problem" (where a class inherits from two classes that both inherit from a common base), Python uses the C3 linearization algorithm to establish a consistent MRO.

The C3 linearization ensures that:
- The subclass appears before its parents.
- Parents appear in the order they are listed.
- A parent class appears before any of its ancestors.

Here’s an example demonstrating the diamond problem and how Python resolves it:

class A:
def process(self):
print("A.process")

class B(A):
def process(self):
print("B.process")

class C(A):
def process(self):
print("C.process")

class D(B, C):
pass

# Check MRO
print("MRO of D:", [cls.__name__ for cls in D.mro()])
# Output: ['D', 'B', 'C', 'A', 'object']

# Call the method
d = D()
d.process()

Output:
MRO of D: ['D', 'B', 'C', 'A', 'object']
B.process

Explanation:
- The D class inherits from B and C, both of which inherit from A.
- Without proper MRO, calling d.process() could lead to ambiguity (e.g., should it call B.process or C.process?).
- Python uses C3 linearization to compute MRO as: D -> B -> C -> A -> object.
- Since B comes before C in the inheritance list, B.process is called first.
- This avoids the diamond problem by ensuring a deterministic and predictable order.

This mechanism allows developers to write complex class hierarchies without runtime ambiguity, making Python's multiple inheritance safe and usable.

By: @DataScienceQ 🚀
1
Interview question

What is Dependency Injection and how is it used in Python?

Answer: Dependency Injection is a technique where an object receives external dependencies (such as classes, functions, settings) through parameters rather than creating them internally.

In Python, DI is most often implemented explicitly: dependencies are passed to constructors, functions, or arguments, which increases code modularity and facilitates testing. For example, you can easily replace a service with a mock during unit testing.

Unlike Java, where DI containers like Spring are common, Python usually uses explicit dependency passing but can use libraries like dependency-injector for more complex automation if needed.


tags: #interview

@DataScienceQ 🌸
Please open Telegram to view this post
VIEW IN TELEGRAM
3
Interview Question

What are the parts of an HTTP request?

Answer: An HTTP request consists of a start line (defines the type of message), headers (transmit transfer parameters and message characteristics), and the message body (contains data, separated from the headers by a blank line).

tags: #interview

@DataScienceQ 🤎
Please open Telegram to view this post
VIEW IN TELEGRAM
4
Interview question
What is the difference between numpy.array() and numpy.asarray() when converting a Python list to a NumPy array, and how does it affect memory usage?

Answer:
numpy.array() always creates a new copy of the input data, meaning that modifications to the original list will not affect the resulting array. This ensures data isolation but increases memory usage. In contrast, numpy.asarray() only creates a copy if the input is not already a NumPy array or compatible format—otherwise, it returns a view of the existing data. This makes asarray() more memory-efficient when working with existing arrays or array-like objects. For example, if you pass an existing NumPy array to asarray(), it returns the same object without copying, whereas array() would still create a new copy even if the input is already a NumPy array

tags: #Python #NumPy #MemoryManagement #DataConversion #ArrayOperations #InterviewQuestion

By: @DataScienceQ 🚀
4
Interview question

What is the primary purpose of using np.frombuffer() in NumPy, and how does it handle memory views when dealing with structured arrays?

Answer:
The function np.frombuffer() allows creating a NumPy array from a buffer object, such as a bytes object or memoryview, without copying the data. It interprets the raw bytes according to a specified dtype. When used with structured arrays, it relies on the exact byte layout defined by the dtype, which can lead to unexpected behavior if the structure doesn't align with the actual memory representation, especially across different architectures or endianness. This makes it powerful but risky for low-level data manipulation.

tags: #numpy #python #memoryview #structuredarrays #frombuffer #lowlevel #datainterpretation

By: @DataScienceQ 🚀
Please open Telegram to view this post
VIEW IN TELEGRAM
2
Interview Question

What is a list comprehension in Python and how does it work?

Answer: A list comprehension is a concise way to create lists in Python by applying an expression to each item in an iterable, optionally with a condition (e.g., [x**2 for x in range(10) if x % 2 == 0]), making code more readable and efficient than traditional for loops for generating lists.

tags: #interview

➡️ @DataScienceQ ⭐️
Please open Telegram to view this post
VIEW IN TELEGRAM
Interview Question

What is the difference between __str__ and __repr__ methods in Python classes, and when would you implementstr
__str__ returns a human-readable string representation of an object (e.g., via print(obj)), making it user-friendly for displayrepr__repr__ aims for a more detailed, unambiguous string that's ideally executable as code (like repr(obj)), useful for debugging—imstr __str__ for end-user outrepr__repr__ for developer tools or str __str__ is defined.

tags: #interview #python #magicmethods #classes

➡️ @DataScienceQ 🤎
Please open Telegram to view this post
VIEW IN TELEGRAM
Interview Question

Explain the concept of generators in Python and how they differ from regular iterators in terms of memory efficiency.

Generators are functions that use yield to produce a sequence of values lazily (e.g., def gen(): yield 1; yield 2), creating an iterator that generates items on-the-fly without storing the entire sequence in memory, unlike regular iterators or lists which can consume more RAM for large datasets—ideal for processing big data streams efficiently.

tags: #interview #python #generators #memory

@DataScienceQ ⭐️
Please open Telegram to view this post
VIEW IN TELEGRAM
Forwarded from Code With Python
In Python, you can unpack sequences using *, to work with a variable number of elements. The * can be placed anywhere and it will collect all the extra elements into a separate variable.

a, b, c = 10, 2, 3      # Standard unpacking

a, *b = 10, 2, 3        # b = [2, 3]

a, *b, c = 10, 2, 3, 4  # b = [2, 3]

*a, b, c = 10, 2, 3, 4  # a = [10, 2]


👉  @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
In Python, list comprehensions provide a concise way to create lists by applying an expression to each item in an iterable, optionally with conditions. They're more readable and efficient than loops for transformations.

squares = [x**2 for x in range(10)]  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

evens = [x for x in range(20) if x % 2 == 0] # [0, 2, 4,..., 18]


👍 @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
In Python, multiple inheritance allows a class to inherit from more than one parent class, enabling complex hierarchies but requiring careful management of method resolution order (MRO) to avoid conflicts. The MRO is determined using C3 linearization and can be inspected via the __mro__ attribute or mro() method.

class A:
def greet(self):
return "Hello from A"

class B:
def greet(self):
return "Hello from B"

class C(A, B): # Inherits from A then B
pass

c = C()
print(c.greet()) # "Hello from A" (A's method first in MRO)
print(C.__mro__) # (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)


🏐 @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
In Python, abstract base classes (ABCs) in the abc module define interfaces for subclasses to implement, enforcing polymorphism and preventing instantiation of incomplete classes. Use them for designing robust class hierarchies where specific methods must be overridden.

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

# rect = Rectangle(5, 3)
# print(rect.area()) # 15
# Shape() # Error: Can't instantiate abstract class


#python #OOP #classes #abc #inheritance

👉 @DataScience4
3
In Python, lists are versatile mutable sequences with built-in methods for adding, removing, searching, sorting, and more—covering all common scenarios like dynamic data manipulation, queues, or stacks. Below is a complete breakdown of all list methods, each with syntax, an example, and output, plus key built-in functions for comprehensive use.

📚 Adding Elements
append(x): Adds a single element to the end.

  lst = [1, 2]
lst.append(3)
print(lst) # Output: [1, 2, 3]


extend(iterable): Adds all elements from an iterable to the end.

  lst = [1, 2]
lst.extend([3, 4])
print(lst) # Output: [1, 2, 3, 4]


insert(i, x): Inserts x at index i (shifts elements right).

  lst = [1, 3]
lst.insert(1, 2)
print(lst) # Output: [1, 2, 3]


📚 Removing Elements
remove(x): Removes the first occurrence of x (raises ValueError if not found).

  lst = [1, 2, 2]
lst.remove(2)
print(lst) # Output: [1, 2]


pop(i=-1): Removes and returns the element at index i (default: last).

  lst = [1, 2, 3]
item = lst.pop(1)
print(item, lst) # Output: 2 [1, 3]


clear(): Removes all elements.

  lst = [1, 2, 3]
lst.clear()
print(lst) # Output: []


📚 Searching and Counting
count(x): Returns the number of occurrences of x.

  lst = [1, 2, 2, 3]
print(lst.count(2)) # Output: 2


index(x[, start[, end]]): Returns the lowest index of x in the slice (raises ValueError if not found).

  lst = [1, 2, 3, 2]
print(lst.index(2)) # Output: 1


📚 Ordering and Copying
sort(key=None, reverse=False): Sorts the list in place (ascending by default; stable sort).

  lst = [3, 1, 2]
lst.sort()
print(lst) # Output: [1, 2, 3]


reverse(): Reverses the elements in place.

  lst = [1, 2, 3]
lst.reverse()
print(lst) # Output: [3, 2, 1]


copy(): Returns a shallow copy of the list.

  lst = [1, 2]
new_lst = lst.copy()
print(new_lst) # Output: [1, 2]


📚 Built-in Functions for Lists (Common Cases)
len(lst): Returns the number of elements.

  lst = [1, 2, 3]
print(len(lst)) # Output: 3


min(lst): Returns the smallest element (raises ValueError if empty).

  lst = [3, 1, 2]
print(min(lst)) # Output: 1


max(lst): Returns the largest element.

  lst = [3, 1, 2]
print(max(lst)) # Output: 3


sum(lst[, start=0]): Sums the elements (start adds an offset).

  lst = [1, 2, 3]
print(sum(lst)) # Output: 6


sorted(lst, key=None, reverse=False): Returns a new sorted list (non-destructive).

  lst = [3, 1, 2]
print(sorted(lst)) # Output: [1, 2, 3]


These cover all standard operations (O(1) for append/pop from end, O(n) for most others). Use slicing lst[start:end:step] for advanced extraction, like lst[1:3] outputs ``.

#python #lists #datastructures #methods #examples #programming

@DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
3
In Python, for loops are versatile for iterating over iterables like lists, strings, or ranges, but advanced types include basic iteration, index-aware with enumerate(), parallel with zip(), nested for multi-level data, and comprehension-based—crucial for efficient data processing in interviews without overcomplicating.

# Basic for loop over iterable (list)
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterates each element directly
print(fruit) # Output: apple \n banana \n cherry

# For loop with range() for numeric sequences
for i in range(3): # Generates 0, 1, 2 (start=0, stop=3, step=1)
print(i) # Output: 0 \n 1 \n 2

for i in range(1, 6, 2): # Start=1, stop=6, step=2
print(i) # Output: 1 \n 3 \n 5

# Index-aware with enumerate() (gets both index and value)
for index, fruit in enumerate(fruits, start=1): # start=1 for 1-based indexing
print(f"{index}: {fruit}") # Output: 1: apple \n 2: banana \n 3: cherry

# Parallel iteration with zip() (pairs multiple iterables)
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages): # Stops at shortest iterable
print(f"{name} is {age} years old") # Output: Alice is 25 years old \n Bob is 30 years old \n Charlie is 35 years old

# Nested for loops (outer for rows, inner for columns; e.g., matrix)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix: # Outer: each sublist
for num in row: # Inner: each element in row
print(num, end=' ') # Output: 1 2 3 4 5 6 7 8 9 (space-separated)

# For loop in list comprehension (concise iteration with optional condition)
squares = [x**2 for x in range(5)] # Basic comprehension
print(squares) # Output: [0, 1, 4, 9, 16]

evens_squared = [x**2 for x in range(10) if x % 2 == 0] # With condition (if)
print(evens_squared) # Output: [0, 4, 16, 36, 64]

# Nested comprehension (flattens 2D list)
flattened = [num for row in matrix for num in row] # Equivalent to nested for
print(flattened) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]


#python #forloops #range #enumerate #zip #nestedloops #listcomprehension #interviewtips #iteration

👉 @DataScience4
3
In Python, loops are essential for repeating code efficiently: for loops iterate over known sequences (like lists or ranges) when you know the number of iterations, while loops run based on a condition until it's false (ideal for unknown iteration counts or sentinel values), and nested loops handle multi-dimensional data by embedding one inside another—use break/continue for control, and comprehensions for concise alternatives in interviews.

# For loop: Use for fixed iterations over iterables (e.g., processing lists)
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterates each element
print(fruit) # Output: apple \n banana \n cherry

for i in range(3): # Numeric sequence (start=0, stop=3)
print(i) # Output: 0 \n 1 \n 2

# While loop: Use when iterations depend on a dynamic condition (e.g., user input, convergence)
count = 0
while count < 3: # Runs as long as condition is True
print(count)
count += 1 # Increment to avoid infinite loop! Output: 0 \n 1 \n 2

# Nested loops: Use for 2D data (e.g., matrices, grids); outer for rows, inner for columns
matrix = [[1, 2], [3, 4]]
for row in matrix: # Outer: each sublist
for num in row: # Inner: elements in row
print(num) # Output: 1 \n 2 \n 3 \n 4

# Control statements: break (exit loop), continue (skip iteration)
for i in range(5):
if i == 2:
continue # Skip 2
if i == 4:
break # Exit at 4
print(i) # Output: 0 \n 1 \n 3

# List comprehension: Concise for loop alternative (use for simple transformations/filtering)
squares = [x**2 for x in range(5) if x % 2 == 0] # Even squares
print(squares) # Output: [0, 4, 16]


#python #loops #forloop #whileloop #nestedloops #comprehensions #interviewtips #controlflow

👉 @DataScience4
In Python, loops are essential for repeating code efficiently: for loops iterate over known sequences (like lists or ranges) when you know the number of iterations, while loops run based on a condition until it's false (ideal for unknown iteration counts or sentinel values), and nested loops handle multi-dimensional data by embedding one inside another—use break/continue for control, and comprehensions for concise alternatives in interviews.

# For loop: Use for fixed iterations over iterables (e.g., processing lists)
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterates each element
print(fruit) # Output: apple \n banana \n cherry

for i in range(3): # Numeric sequence (start=0, stop=3)
print(i) # Output: 0 \n 1 \n 2

# While loop: Use when iterations depend on a dynamic condition (e.g., user input, convergence)
count = 0
while count < 3: # Runs as long as condition is True
print(count)
count += 1 # Increment to avoid infinite loop! Output: 0 \n 1 \n 2

# Nested loops: Use for 2D data (e.g., matrices, grids); outer for rows, inner for columns
matrix = [[1, 2], [3, 4]]
for row in matrix: # Outer: each sublist
for num in row: # Inner: elements in row
print(num) # Output: 1 \n 2 \n 3 \n 4

# Control statements: break (exit loop), continue (skip iteration)
for i in range(5):
if i == 2:
continue # Skip 2
if i == 4:
break # Exit at 4
print(i) # Output: 0 \n 1 \n 3

# List comprehension: Concise for loop alternative (use for simple transformations/filtering)
squares = [x**2 for x in range(5) if x % 2 == 0] # Even squares
print(squares) # Output: [0, 4, 16]


#python #loops #forloop #whileloop #nestedloops #comprehensions #interviewtips #controlflow

👉 https://t.iss.one/CodeProgrammer
2
In Python, arrays (lists) support powerful unpacking with * to handle variable-length sequences. This allows flexible assignment and manipulation of elements from lists or other iterable types.

a, b, c = [10, 2, 3]        # Standard unpacking  
a, *b = [10, 2, 3] # b = [2, 3]
a, *b, c = [10, 2, 3, 4] # b = [2, 3]
*a, b, c = [10, 2, 3, 4] # a = [10, 2]


# Built-in Functions with Arrays

### len() – Get array length
arr = [1, 2, 3, 4]
print(len(arr)) # Output: 4


### max() & min() – Find extremes
arr = [5, 1, 8, 3]
print(max(arr)) # Output: 8
print(min(arr)) # Output: 1


### sum() – Sum all elements
arr = [1, 2, 3, 4]
print(sum(arr)) # Output: 10


### sorted() – Return sorted copy
arr = [3, 1, 4, 2]
print(sorted(arr)) # Output: [1, 2, 3, 4]
print(sorted(arr, reverse=True)) # Output: [4, 3, 2, 1]


### list() – Convert to list
tup = (1, 2, 3)
lst = list(tup)
print(lst) # Output: [1, 2, 3]


### append() – Add item at end
arr = [1, 2]
arr.append(3)
print(arr) # Output: [1, 2, 3]


### extend() – Add multiple items
arr = [1, 2]
arr.extend([3, 4])
print(arr) # Output: [1, 2, 3, 4]


### insert() – Insert at specific index
arr = [1, 2, 4]
arr.insert(2, 3)
print(arr) # Output: [1, 2, 3, 4]


### remove() – Remove first occurrence
arr = [1, 2, 3, 2]
arr.remove(2)
print(arr) # Output: [1, 3, 2]


### pop() – Remove and return item
arr = [1, 2, 3]
val = arr.pop()
print(val) # Output: 3
print(arr) # Output: [1, 2]


### index() – Find element position
arr = [10, 20, 30]
print(arr.index(20)) # Output: 1


### count() – Count occurrences
arr = [1, 2, 2, 3]
print(arr.count(2)) # Output: 2


### reverse() – Reverse in-place
arr = [1, 2, 3]
arr.reverse()
print(arr) # Output: [3, 2, 1]


### copy() – Shallow copy
arr = [1, 2, 3]
new_arr = arr.copy()
print(new_arr) # Output: [1, 2, 3]

By: @DataScienceQ 🔗
Please open Telegram to view this post
VIEW IN TELEGRAM
1