🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Swapping two elements in a list is a fundamental operation in computer science, often used in sorting algorithms and data manipulation tasks.
Swapping refers to the action of taking two positions in a list (or array) and exchanging their contents. This operation is often used to rearrange lists or during algorithms that require elements to change places, like bubble sort or quicksort.
2. Program Steps
1. Define the list and the indices of the elements to be swapped.
2. Use tuple unpacking to swap the elements at these indices.
3. Verify the list after the swap.
3. Code Program
# Function to swap two elements in a list
def swap_elements(a_list, index1, index2):
# Swap the elements using tuple unpacking
a_list[index1], a_list[index2] = a_list[index2], a_list[index1]
return a_list
# List to perform the swap on
my_list = [23, 65, 19, 90]
# Indices of the elements to swap
index1, index2 = 1, 3
# Call the function and print the modified list
swapped_list = swap_elements(my_list, index1, index2)
print(f"List after swapping: {swapped_list}")
Output:
List after swapping: [23, 90, 19, 65]
Explanation:
1. swap_elements is a function that takes a list a_list and two indices index1 and index2.
2. Inside the function, the elements at positions index1 and index2 are swapped using tuple unpacking.
3. my_list is initially defined with 4 elements.
4. index1 and index2 are set to 1 and 3, representing the positions of the elements to swap.
5. The swap_elements function is called with my_list, index1, and index2.
6. After the swap, the function returns the modified list, and it is printed, showing that the elements at positions 1 and 3 have been swapped.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment