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

list in python

The document outlines the features and functionalities of lists in Python, highlighting their ordered, mutable, and heterogeneous nature. It covers key operations such as accessing, adding, removing, modifying elements, and supports various methods for sorting, iterating, and manipulating lists. Additionally, it discusses advanced topics like nested lists, membership testing, and removing duplicates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

list in python

The document outlines the features and functionalities of lists in Python, highlighting their ordered, mutable, and heterogeneous nature. It covers key operations such as accessing, adding, removing, modifying elements, and supports various methods for sorting, iterating, and manipulating lists. Additionally, it discusses advanced topics like nested lists, membership testing, and removing duplicates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Features of Lists in Python

A list is a versatile and widely used data structure in Python. It is a mutable, ordered, and
iterable collection that can store elements of any data type.

Key Features of Lists

1. Ordered
o Lists maintain the order of the elements.
o Example:

my_list = [10, 20, 30]

print(my_list[0]) # Output: 10 (order is preserved)

2. Mutable

Lists can be modified after creation. You can add, remove, or change elements.

Example

my_list = [1, 2, 3]

my_list[1] = 20

print(my_list) # Output: [1, 20, 3]

3. Heterogeneous

A list can contain elements of different data types (e.g., integers, strings, floats, etc.).

Example:
my_list = [1, "hello", 3.14]

print(my_list) # Output: [1, "hello", 3.14]

4. Dynamic

Lists are dynamic in size, meaning you can add or remove elements at any time.
Example:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]

5. Indexed
Lists use zero-based indexing, allowing direct access to elements.
my_list = [10, 20, 30]
print(my_list[1]) # Output: 20
6. Supports Negative Indexing
you can access elements using negative indices, where -1 refers to the last element, -2 to
the second last, and so on.

my_list = [10, 20, 30]


print(my_list[-1]) # Output: 30

7. Supports Nesting

 Lists can contain other lists as elements (nested lists).

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


print(my_list[1]) # Output: [3, 4]

8. Iterable

 Lists can be iterated over using loops, comprehensions, or other iterable operations.

my_list = [1, 2, 3]
for x in my_list:
print(x)
# Output: 1 2 3

9. Supports Slicing

you can extract sublists using slicing (start:end:step).

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

print(my_list[1:4]) # Output: [1, 2, 3]

Advantages of Lists

 Ease of Use: Lists provide simple syntax for operations like addition, deletion, and
iteration.
 Flexibility: They can store any data type, including mixed types and nested structures.
 Rich Functionality: With built-in methods and list comprehensions, lists are highly
versatile for a wide range of use cases.
Accessing Elements

a. Indexing (Zero-based)
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10
print(my_list[3]) # Output: 40

b. Negative Indexing
print(my_list[-1]) # Output: 50 (last element)
print(my_list[-3]) # Output: 30 (third element from the end)

c. Slicing
print(my_list[1:4]) # Output: [20, 30, 40]
print(my_list[:3]) # Output: [10, 20, 30]
print(my_list[::2]) # Output: [10, 30, 50] (step of 2)

2. Adding Elements

a. Append an Element

Adds an element to the end of the list.

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

b. Insert an Element

Inserts an element at a specific index.

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

c. Extend a List

Adds all elements of another list.

my_list.extend([5, 6])
print(my_list) # Output: [1, 100, 2, 3, 4, 5, 6]

3. Removing Elements

a. Remove an Element

Removes the first occurrence of a value.

my_list.remove(100)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
b. Pop an Element

Removes and returns the element at a specific index (default: last element).

last_element = my_list.pop()
print(last_element) # Output: 6
print(my_list) # Output: [1, 2, 3, 4, 5]

c. Clear the List

Removes all elements.

my_list.clear()
print(my_list) # Output: []

4. Modifying Elements

a. Replace an Element
my_list = [1, 2, 3, 4, 5]
my_list[2] = 300
print(my_list) # Output: [1, 2, 300, 4, 5]

b. Replace Multiple Elements


my_list[1:3] = [20, 30]
print(my_list) # Output: [1, 20, 30, 4, 5]

5. Sorting and Reversing

a. Sort a List

Sorts in ascending order.

my_list = [5, 2, 9, 1]
my_list.sort()
print(my_list) # Output: [1, 2, 5, 9]

b. Sort in Descending Order


my_list.sort(reverse=True)
print(my_list) # Output: [9, 5, 2, 1]

c. Reverse a List

Reverses the elements of the list.

my_list.reverse()
print(my_list) # Output: [1, 2, 5, 9] (reversed order)
6. Copying a List
original_list = [1, 2, 3]
copied_list = original_list.copy()
print(copied_list) # Output: [1, 2, 3]

7. Mathematical Operations

a. Sum of Elements
my_list = [1, 2, 3, 4]
print(sum(my_list)) # Output: 10

b. Find Maximum and Minimum


print(max(my_list)) # Output: 4
print(min(my_list)) # Output: 1

c. Find the Average


average = sum(my_list) / len(my_list)
print(average) # Output: 2.5

8. Membership Testing

a. Check if an Element Exists


my_list = [1, 2, 3, 4]
print(3 in my_list) # Output: True
print(5 in my_list) # Output: False

9. Iterating Over a List

a. Using Loops
for item in my_list:
print(item)

b. List Comprehension
squares = [x**2 for x in my_list]
print(squares) # Output: [1, 4, 9, 16]

10. Nested Lists


nested_list = [[1, 2], [3, 4], [5, 6]]
print(nested_list[1][0]) # Output: 3

11. Other Operations

a. Count Occurrences of an Element


my_list = [1, 2, 2, 3, 4, 2]
print(my_list.count(2)) # Output: 3
b. Find Index of an Element
print(my_list.index(3)) # Output: 3

12. Joining and Splitting Lists

a. Join a List into a String


words = ['Hello', 'World']
sentence = ' '.join(words)
print(sentence) # Output: "Hello World"

b. Split a String into a List


sentence = "Hello World"
words = sentence.split()
print(words) # Output: ['Hello', 'World']

13.With sorted() (creates a new sorted list):


my_list = [3, 1, 4, 2, 5]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)

Output:

[5, 4, 3, 2, 1]
The reverse=True parameter tells Python to sort the list in descending
order.

14. Remove Duplicates from a List

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
print("List after removing duplicates:", unique_list)

Output (order may vary due to set):

List after removing duplicates: [1, 2, 3, 4, 5]

15.Find Even and Odd Numbers


my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = [x for x in my_list if x % 2 == 0]
odd_numbers = [x for x in my_list if x % 2 != 0]

print("Even numbers:", even_numbers)


print("Odd numbers:", odd_numbers)
Output:
Even numbers: [2, 4, 6, 8]
Odd numbers: [1, 3, 5, 7, 9]

You might also like