{"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 Output:<\/strong><\/p>\n Integer List:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to sort the first and last value of the given list in python some of them are:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Approach:<\/strong><\/p>\n i)String list<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Integer list<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n i)String list<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Integer list<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists in Python: 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 …<\/p>\ngiven list = ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']<\/pre>\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
given list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]<\/pre>\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
\n
Method #1:Using temporary variable<\/h3>\n
\n
# given list\r\ngiven_list = ['hello', 'this', 'is', 'BTechGeeks',\r\n 'online', 'platform', 'for', 'btech', 'students']\r\n# printing the given list before swapping the values\r\nprint('printing the given list before swapping the values = ')\r\nprint(given_list)\r\n# Swap the first and last elements in the given list using a temporary variable.\r\ntempo = given_list[0]\r\ngiven_list[0] = given_list[-1]\r\ngiven_list[-1] = tempo\r\n# printing the given list after swapping the values\r\nprint('printing the given list after swapping the values = ')\r\nprint(given_list)\r\n<\/pre>\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
# given list\r\ngiven_list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]\r\n# printing the given list before swapping the values\r\nprint('printing the given list before swapping the values = ')\r\nprint(given_list)\r\n# Swap the first and last elements in the given list using a temporary variable.\r\ntempo = given_list[0]\r\ngiven_list[0] = given_list[-1]\r\ngiven_list[-1] = tempo\r\n# printing the given list after swapping the values\r\nprint('printing the given list after swapping the values = ')\r\nprint(given_list)\r\n<\/pre>\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
Method #2: Using comma operator in Python<\/h3>\n
\n
# given list\r\ngiven_list = ['hello', 'this', 'is', 'BTechGeeks',\r\n 'online', 'platform', 'for', 'btech', 'students']\r\n# printing the given list before swapping the values\r\nprint('printing the given list before swapping the values = ')\r\nprint(given_list)\r\n# Swap the first and last elements in the given list using a , operator\r\ngiven_list[0], given_list[-1] = given_list[-1], given_list[0]\r\n# printing the given list after swapping the values\r\nprint('printing the given list after swapping the values = ')\r\nprint(given_list)\r\n<\/pre>\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
# given list\r\ngiven_list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]\r\n# printing the given list before swapping the values\r\nprint('printing the given list before swapping the values = ')\r\nprint(given_list)\r\n# Swap the first and last elements in the given list using a , operator\r\ngiven_list[0], given_list[-1] = given_list[-1], given_list[0]\r\n# printing the given list after swapping the values\r\nprint('printing the given list after swapping the values = ')\r\nprint(given_list)\r\n<\/pre>\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
\n