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

Python_List

A Python list is an ordered, mutable collection that allows duplicate values and can contain elements of different data types. Lists support various operations such as indexing, slicing, modifying, and looping, making them versatile for storing and managing data. They are commonly used for applications like handling dynamic data, representing matrices, and efficient searching and sorting.

Uploaded by

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

Python_List

A Python list is an ordered, mutable collection that allows duplicate values and can contain elements of different data types. Lists support various operations such as indexing, slicing, modifying, and looping, making them versatile for storing and managing data. They are commonly used for applications like handling dynamic data, representing matrices, and efficient searching and sorting.

Uploaded by

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

Python List - Detailed Explanation

Definition of a List:
A list in Python is an ordered, mutable collection of elements that
allows duplicate values. Lists are
one of the most commonly used data structures in Python and can
hold elements of different data types.

Characteristics of a List:
- Ordered: The order of elements is maintained.
- Mutable: Elements can be modified, added, or removed.
- Allows Duplicates: A list can contain repeated elements.
- Heterogeneous: A list can contain different data types.
- Indexing & Slicing Supported: Elements can be accessed via their
index.
- Dynamic Size: Lists can grow or shrink as needed.

Creating a List:
1. Using Square Brackets []
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
mixed = [10, "hello", 3.14, True]

2. Using list() Constructor


vowels = list(("a", "e", "i", "o", "u"))

3. Creating an Empty List


empty_list = []

Accessing List Elements:


1. Indexing (Starts from 0)
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple

2. Negative Indexing (Counts from the end)


print(fruits[-1]) # Output: cherry

3. Slicing (Extracts a portion of the list)


numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]

Modifying a List:
1. Changing an Element
fruits[1] = "mango"

2. Adding Elements:
fruits.append("orange") # Adds to the end
fruits.insert(1, "grape") # Adds at a specific index

3. Removing Elements:
fruits.remove("apple") # Removes first occurrence
fruits.pop() # Removes the last element

List Operations:
1. Concatenation (+)
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2

2. Repetition (*)
numbers = [1, 2, 3] * 3

3. Membership Check (in operator)


print(2 in numbers) # Output: True
4. Finding the Length of a List (len())
print(len(numbers)) # Output: 3

Looping Through a List:


1. Using for Loop
for fruit in fruits:
print(fruit)

2. Using while Loop


i=0
while i < len(fruits):
print(fruits[i])
i += 1

Sorting a List:
1. Using sort()
numbers.sort()

2. Using sorted()
sorted_numbers = sorted(numbers)

Reversing a List:
numbers.reverse()

Copying a List:
1. Using copy()
list_copy = numbers.copy()

2. Using Slicing [:]


list_copy = numbers[:]

Nested Lists:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2]) # Output: 6

Applications of Lists:
1. Storing a collection of items
2. Handling dynamic data
3. Representing matrices or graphs
4. Efficient searching and sorting
5. Processing data using loops

Summary:
- Ordered collection
- Mutable (changeable)
- Allows duplicates
- Supports slicing, sorting, etc.
- Stores different data types
- Supports list comprehension for efficiency

You might also like