Python - Lists

17.
How do you iterate over elements in a list using a for loop?

In Python, you can iterate over elements in a list using a for loop. The loop iterates over each element in the list, allowing you to perform actions on each element one at a time. Here's an example program:

# Creating a list
my_list = [1, 2, 3, 4, 5]

# Iterating over elements in the list using a for loop
for element in my_list:
    # Performing an action on each element
    print("Element:", element)

The program initializes a list (my_list) and uses a for loop to iterate over each element in the list. Inside the loop, an action (printing the element) is performed for each iteration.

Output:

Element: 1
Element: 2
Element: 3
Element: 4
Element: 5

Another way to iterate over elements in a list is by using the range() function and the length of the list. This approach allows you to access elements using their index. Here's an example:

# Creating a list
my_list = [1, 2, 3, 4, 5]

# Iterating over elements in the list using range and length
for i in range(len(my_list)):
    # Accessing elements by index and performing an action
    print("Element at index", i, ":", my_list[i])

Output:

Element at index 0 : 1
Element at index 1 : 2
Element at index 2 : 3
Element at index 3 : 4
Element at index 4 : 5

18.
Discuss the use of the zip() function with lists in Python.

The zip() function in Python is used to combine elements from multiple iterables (e.g., lists) into tuples. It returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the input iterables. Let's explore the use of the zip() function with lists:

# Creating two lists
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 22]

# Using zip() to combine elements from both lists into tuples
zipped_data = zip(names, ages)

# Displaying the result
for data in zipped_data:
    print("Name:", data[0], "| Age:", data[1])

The program creates two lists, names and ages, and uses the zip() function to combine their elements into tuples. The resulting tuples are then iterated over using a for loop, and the elements are displayed.

Output:

Name: Alice | Age: 25
Name: Bob | Age: 30
Name: Charlie | Age: 22

19.
How can you create a nested list in Python?

In Python, you can create a nested list by including one or more lists as elements within another list. This results in a structure where each element of the outer list is itself a list. Here's an example program:

# Creating a nested list
nested_list = [1, [2, 3], [4, 5, 6], 7]

# Displaying the nested list
print(nested_list)

The program creates a nested list (nested_list) with various elements, including individual values and inner lists. The resulting structure is a list within a list.

Output:

[1, [2, 3], [4, 5, 6], 7]

20.
Explain the purpose of the clear() method in Python lists.

The clear() method in Python lists is used to remove all elements from the list. It effectively empties the list, leaving it with a length of 0. Here's an example program that demonstrates the use of the clear() method:

# Creating a list
my_list = [1, 2, 3, 4, 5]

# Displaying the original list
print("Original List:", my_list)

# Using clear() to remove all elements from the list
my_list.clear()

# Displaying the list after clear()
print("List after clear():", my_list)

The program creates a list (my_list) with some elements, displays the original list, and then uses the clear() method to remove all elements. The list is then displayed again to show that it is now empty.

Outputs:

Original List: [1, 2, 3, 4, 5]
List after clear(): []