In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
In Python, a list is a built-in data structure that is used to store multiple
items in a single variable. Lists are ordered, mutable (changeable), and
can contain elements of any data type, including other lists (making them
nested). Lists are defined using square brackets [], and the items inside the
list are separated by commas.
Key Characteristics of Lists:
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
2. Mutable: You can modify lists by adding, removing, or updating
elements.
3. Heterogeneous: Lists can contain elements of different types
(integers, strings, other lists, etc.).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
# Example of creating a list
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
Accessing List Elements:
You can access individual elements using their index.
python
Copy code
print(my_list[0]) # Output: 1 (first element)
print(my_list[3]) # Output: 'hello' (fourth element)
print(my_list[-1]) # Output: [4, 5] (last element using negative indexing)
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
my_list[1] = 'Python' # Change second element
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5]]
Adding elements:
python
Copy code
my_list.append(6) # Add 6 to the end of the list
print(my_list) # Output: [1, 'Python', 3, 'hello', [4, 5], 6]
Removing elements:
python
Copy code
my_list.remove('hello') # Remove the element 'hello'
print(my_list) # Output: [1, 'Python', 3, [4, 5], 6]
Common List Operations:
1. Appending an element: append() adds an item to the end of the list.
python
Copy code
my_list.append(10)
2. Inserting an element: insert() adds an element at a specific position.
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
3. Removing an element: remove() removes the first occurrence of an
element.
python
Copy code
my_list.remove(3)
4. Popping an element: pop() removes and returns the element at a
given index (or the last item if no index is provided).
python
Copy code
my_list.pop() # Removes and returns the last element
5. Length of a list: len() returns the number of elements in the list.
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Accessing list elements
print(fruits[1]) # Output: 'banana'
# Modifying list elements
fruits[0] = 'mango' # Changing 'apple' to 'mango'
print(fruits) # Output: ['mango', 'banana', 'cherry']
# Adding and removing elements
fruits.append('orange')
fruits.remove('banana')
print(fruits) # Output: ['mango', 'cherry', 'orange']
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.