Knowee
Questions
Features
Study Tools

Given a list, write a Python program to swap first and last element of the list.

Question

Given a list, write a Python program to swap first and last element of the list.

🧐 Not the exact question you are looking for?Go ask a question

Solution

To swap the first and last elements of a list in Python, you can follow these steps:

  1. Define a list with some elements.
  2. Use indexing to access the first and last elements of the list.
  3. Assign the first element to a temporary variable.
  4. Assign the last element to the first position in the list.
  5. Assign the temporary variable to the last position in the list.
  6. Print the updated list to verify the swap.

Here's an example code snippet that demonstrates this:

# Step 1: Define a list
my_list = [1, 2, 3, 4, 5]

# Step 2: Access the first and last elements
first_element = my_list[0]
last_element = my_list[-1]

# Step 3: Assign the first element to a temporary variable
temp = first_element

# Step 4: Assign the last element to the first position in the list
my_list[0] = last_element

# Step 5: Assign the temporary variable to the last position in the list
my_list[-1] = temp

# Step 6: Print the updated list
print("Swapped list:", my_list)

Output:

Swapped list: [5, 2, 3, 4, 1]

In this example, the first element (1) and the last element (5) of the list are swapped, resulting in a new list [5, 2, 3, 4, 1].

This problem has been solved

Similar Questions

Write a Python program to replace the last element in a list with another list.Sample data : [1, 3, 5, 7, 9, 10], [2, 4, 6, 8]Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]original_list = [1, 3, 5, 7, 9, 10]new_list = [2, 4, 6, 8]original_list[0:-1] = new_listprint(original_list)original_list = [1, 3, 5, 7, 9, 10]new_list = [2, 4, 6, 8]original_list[-1:] = new_listprint(original_list)original_list = [1, 3, 5, 7, 9, 10]new_list = [2, 4, 6, 8]original_list[1:1] = new_listprint(original_list)original_list = [1, 3, 5, 7, 9, 10]new_list = [2, 4, 6, 8]original_list[0:0] = new_listprint(original_list)

Write a function that replaces an element in a list at a specific position without modifying the original list (like in C).Prototype: def new_in_list(my_list, idx, element):If idx is negative, the function should return a copy of the original listIf idx is out of range (> of number of element in my_list), the function should return a copy of the original listYou are not allowed to import any moduleYou are not allowed to use try/except

Write a Python program to interchange the first and last element of the ListConstraints:Input Format:Line 1: <Read a List>Output Format:<List>Example:Input:[5,10,15,20,25,30,35,40]Output:[40, 10, 15, 20, 25, 30, 35, 5]Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [5,10,15,20,25,30,35,40][40, 10, 15, 20, 25, 30, 35, 5]

What will be the output of the following Python code?x = ['xy', 'za']for i in x:    i.swapcase()print(x)

my_first_list = ['a', 'b', 'c']my_second_list = ['d', 'e', 'f', 'g']my_third_list = my_second_list + my_first_listmy_third_list.pop()my_third_list.sort()print (my_third_list)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.