{"id":9248,"date":"2021-09-30T16:30:58","date_gmt":"2021-09-30T11:00:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=9248"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-to-swap-the-first-and-last-value-of-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-swap-the-first-and-last-value-of-a-list\/","title":{"rendered":"Python Program to Swap the First and Last Value of a List"},"content":{"rendered":"

Lists in Python:<\/strong><\/p>\n

A list is exactly what it sounds like: a container for various Python objects such as integers, words, values, and so on. In other programming languages, it is equal to an array. It is denoted with square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, which means it can be changed or altered, as opposed to tuples, which are immutable.<\/p>\n

Given a list , the task is to swap the first and last value of the given list.<\/p>\n

Examples:<\/strong><\/p>\n

String List:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

given list = ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']<\/pre>\n

Output:<\/strong><\/p>\n

printing the given list before swapping the values = \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']\r\nprinting the given list after swapping the values = \r\n['students', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'hello']<\/pre>\n

Integer List:<\/strong><\/p>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

given list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]<\/pre>\n

Output:<\/strong><\/p>\n

printing the given list before swapping the values = \r\n[33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]\r\nprinting the given list after swapping the values = \r\n[129, 92, 12, 74, 22, 79, 122, 17, 63, 99, 33]<\/pre>\n

Program to Swap the First and Last Value of the Given List in Python<\/h2>\n

There are several ways to sort the first and last value of the given list in python some of them are:<\/p>\n