0% found this document useful (0 votes)
2 views

Batch 11 Python

Uploaded by

madhan000183
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Batch 11 Python

Uploaded by

madhan000183
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EASWARI ENGINEERING COLLEGE

(Autonomous)
Bharathi Salai, Ramapuram, Chennai – 89
Department of Computer Science Engineering SDG GOAL NO.4
Group Presentation

Batch -XI
TOPIC : Lists Methods and List Comprehension in Python

TEAM MEMBERS
310624104170- MADHAN KUMAR J
310624104171- MADHAN M
310624104172 - MADHAN V M
310624104173- MADHUMITHA G R
310624104174- MADHUMITHA V
OUTCOMES OF SDG GOALS:
• Enhances Problem-Solving Skills: List methods and comprehension train learners to approach
tasks systematically, breaking problems into smaller, solvable pieces, a critical component of
quality education.
• Fosters Logical Thinking: By applying conditions and iterative logic, students learn structured
thinking, essential for both programming and general analytical problem-solving.

• Promotes Efficiency: List comprehension provides a compact, efficient way to handle data,
teaching students how to write clean, optimized code—a valuable skill in software development
and data science.

• Encourages Creativity: The flexibility of these tools allows students to explore multiple solutions
to a problem, stimulating innovation and adaptability.

• Prepares for Interdisciplinary Applications: These skills are foundational for fields like data
analysis, AI, and scientific computing, preparing students for careers in diverse domains.

• Supports Inclusive Education: Python's simplicity, paired with these features, makes
programming accessible to learners worldwide, bridging gaps in technical education and
contributing to equity in learning opportunities.
Lists, Methods, and
List Comprehension in
Python
Join us on a journey through the world of Python lists,
discovering their power and flexibility in crafting
dynamic and efficient code.
What are Lists in Python?
Ordered Sequences Mutable Versatile

Lists are ordered collections Lists are mutable, meaning Lists can store different
of items, meaning that the you can change their contents data types, including
order in which you add items after they've been created by numbers, strings, and even
is preserved. For example: adding, removing, or other lists, making them
modifying items. Here's an highly versatile. Consider
my_list = [1, "hello", example: this:
mixed_list = [1, "apple",
3.14, True] my_list = [1, 2, 3] [2, 3], 4.5]
print(my_list) # Output: my_list[0] = 10 print(mixed_list) #
[1, "hello", 3.14, True] my_list.append(4) Output: [1, "apple", [2,
print(my_list[0]) # print(my_list) # Output: 3], 4.5]
Output: 1 [10, 2, 3, 4]
Common List Operations and Methods
append() insert() remove()
Adds an element to the end of a list. Inserts an element at a specified index. Removes the first occurrence of a specified element.

my_list = [1, 2, 3] my_list = [1, 2, 3] my_list = [1, 2, 3, 2]


my_list.append(4) my_list.insert(1, 4) my_list.remove(2)
print(my_list) # Output: [1, 2, 3, 4] print(my_list) # Output: [1, 4, 2, 3] print(my_list) # Output: [1, 3, 2]

sort() pop() extend()


Sorts the list in ascending order. Removes and returns the element at a specified Adds all items from an iterable (e.g., another
index (default is the last element). list) to the end of the list.
my_list = [3, 1, 2]
my_list.sort() my_list = [1, 2, 3] my_list = [1, 2, 3]
print(my_list) # Output: [1, 2, 3] popped_element = my_list.pop(1) my_list.extend([4, 5])
print(my_list) # Output: [1, 3] print(my_list) # Output: [1, 2, 3, 4, 5]
print(popped_element) # Output: 2

reverse()
Reverses the order of elements in the list.

my_list = [1, 2, 3]
my_list.reverse()
print(my_list) # Output: [3, 2, 1]
Common List Operations and Methods
Input
OUTPUT
List Comprehension in python
List comprehension in Python is a concise and powerful way to create, filter, and transform lists
using a single line of code. It combines the functionality of loops and conditional statements
into a readable and efficient syntax.

Syntax of List Comprehension:

[expression for item in iterable if condition]

expression: The value or transformation to include in the new list.

item: Each element from the iterable (e.g., list, range, string).

iterable: A sequence or collection to loop over.

condition (optional): A filter that determines whether the item is included.


Filtering and Transforming Lists with List
Comprehension

1 Concise Syntax 2 Efficiency 3 Flexibility


Creates a new list by Often more efficient and Supports filtering and
applying operations to each readable than using transforming elements based
element of an existing list. traditional loops. Consider on conditions. For instance,
For example, to square each this example of filtering convert strings to uppercase
number in a list: even numbers: and filter for those longer
numbers = [1, 2, 3, 4, 5] numbers = [1, 2, 3, 4, 5, than 5 characters:
squared_numbers = [x**2 6]
for x in numbers] even_numbers = [x for x words = ["apple",
print(squared_numbers) # in numbers if x % 2 == 0] "banana", "kiwi",
Output: [1, 4, 9, 16, 25] print(even_numbers) # "orange", "grape"]
Output: [2, 4, 6] long_words =
[word.upper() for word in
words if len(word) > 5]
print(long_words) #
Output: ['BANANA',
Program to explain Library Management using list
Comprehension

OUTPUT
Comparison and Practical Applications
* List comprehensions enhance code readability and reduce boilerplate code, making them a
preferred choice for many developers.

* While list methods provide granular control for in-place modifications and specific operations,
list comprehension excels in creating new lists based on existing ones.

For example:

1. Use list methods when you need to modify an existing list, such as appending elements or removing dup
2. Use list comprehension for transforming or filtering data in a declarative and concise way.
3. Practical applications of these tools include data processing, building algorithms, and
solving problems efficiently. For instance, sorting a list of numbers, extracting specific
elements, or converting raw data into structured formats.
Real-Time Applications of List Methods and Comprehensions
1. Filtering Data (e.g., Extracting Even Numbers) 2.Removing Specific Items (e.g., Removing Negative Numbers)
# List of numbers # List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] numbers = [-1, 2, -3, 4, -5, 6]
# Using list comprehension to extract even numbers # Using list comprehension to remove negative numbers
even_numbers = [num for num in numbers if num % 2 == 0]
positive_numbers = [num for num in numbers if num >= 0]
print(even_numbers)
print(positive_numbers)
OUTPUT OUTPUT
[2, 4, 6, 8]
[2, 4, 6]

3. Cleaning Data (e.g., Removing Non-Alphabetic Characters)


# List of strings
data = ["hello123", "world!", "python_3", "data@science"]
# Using list comprehension to remove non-alphabetic characters
cleaned_data = [''.join([char for char in word if char.isalpha()]) for word in data]
print(cleaned_data)

OUTPUT
['hello', 'world', 'python', 'datascience']
Conclusion
* Both list methods and list comprehensions are indispensable tools in Python.

* While list methods provide a detailed and explicit approach to list manipulation
list comprehension offers a streamlined syntax for creating new lists.

* Mastering these concepts not only simplifies data manipulation but also enhances
the clarity and efficiency of Python programs.
THANK
YOU

You might also like