{"id":3044,"date":"2023-10-20T08:32:44","date_gmt":"2023-10-20T03:02:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=3044"},"modified":"2023-11-10T11:51:25","modified_gmt":"2023-11-10T06:21:25","slug":"python-remove-first-element-from-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-remove-first-element-from-a-list\/","title":{"rendered":"Python: Remove First Element from a List"},"content":{"rendered":"
A list is used in Python to store the sequence of different types of data. Python lists are mutable, which means that their elements can be changed after they have been created. Python, on the other hand, has six data types that can be used to store sequences, with the first two on the list being the most common and accurate.<\/p>\n
A list is a collection of various types of values or objects. The list items are separated by a comma (,), which is enclosed in square brackets [].<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n There are several ways to remove the first item from the list some of them are:<\/p>\n Lists can be cut in Python, as we all know. Slicing is a method of removing the first item from a list. The goal is to make a sublist that contains all of the elements of the list except the first one. Because the slice operation returns a new list, we must assign the new list to the original list. To accomplish this, use the expression l = l[1:], where l is your list. l[1:] is an abbreviation for l[0:len(l)-1].<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Another way to delete an element from a list is to use its index in the del statement. It differs from the pop() function in that it does not return the removed element. Unlike the slicing feature, this does not generate a new list.<\/p>\n The index of first item in list is 0.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The list class in Python has a function called pop(index), which takes an optional argument index and deletes the element at that index. If no justification is given, it deletes the last element of the list by default.<\/p>\n The index of first element in list is 0.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The list class in Python has a function remove(value) that deletes the first occurrence of a given value from the list. This can be used to remove the first item from the list. Select the first element from the list and pass it to the remove() function to accomplish this.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n A double-ended queue, or deque, allows you to add and remove elements from either end. The Deque module is a collection library module. It has methods for adding and removing elements that can be directly invoked with arguments.<\/p>\n Convert the given list to a deque and then pop an element from the left. Then add the remaining deque elements to the list.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n A list is used in Python to store the sequence of different types of data. Python lists are mutable, which means that their elements can be changed after they have been created. Python, on the other hand, has six data types that can be used to store sequences, with the first two on the list …<\/p>\ngivenlist<\/span>\u00a0=<\/span> [\"Hello\", \"Btech\" , \"Geeks\" ,\"is\" ,\"new\" ,\"online\"<\/span>\u00a0]<\/span><\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
Hello is the first element in givenlist hence it is deleted.<\/pre>\n
Remove the First Element of a List<\/h2>\n
\n
>Method #1:Using slicing<\/h3>\n
# Function which removes first element\r\ndef removeFirstElement(givenlist):\r\n # using slicing\r\n givenlist = givenlist[1:]\r\n # return the result list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"Hello\", \"Btech\", \"Geeks\", \"is\", \"new\", \"online\"]\r\n# passing list to remove first element\r\nprint(removeFirstElement(givenlist))\r\n<\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
>Method #2:Using del keyword<\/h3>\n
# Function which removes first element\r\ndef removeFirstElement(givenlist):\r\n # using del keyword\r\n del givenlist[0]\r\n # return the result list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"Hello\", \"Btech\", \"Geeks\", \"is\", \"new\", \"online\"]\r\n# passing list to remove first element\r\nprint(removeFirstElement(givenlist))\r\n<\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
>Method #3:Using pop() function<\/h3>\n
# Function which removes first element\r\ndef removeFirstElement(givenlist):\r\n # using pop function\r\n givenlist.pop(0)\r\n # return the result list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"Hello\", \"Btech\", \"Geeks\", \"is\", \"new\", \"online\"]\r\n# passing list to remove first element\r\nprint(removeFirstElement(givenlist))\r\n<\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
>Method #4:Using remove() function<\/h3>\n
# Function which removes first element\r\ndef removeFirstElement(givenlist):\r\n # getting first element of the given list\r\n firstelement = givenlist[0]\r\n # using remove function\r\n givenlist.remove(firstelement)\r\n # return the result list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"Hello\", \"Btech\", \"Geeks\", \"is\", \"new\", \"online\"]\r\n# passing list to remove first element\r\nprint(removeFirstElement(givenlist))\r\n<\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
>Method #5:Using deque() function<\/h3>\n
from collections import deque\r\n# Function which removes first element\r\n\r\n\r\ndef removeFirstElement(givenlist):\r\n # converting given list to deque\r\n dequelist = deque(givenlist)\r\n # using popleft function to remove first element\r\n dequelist.popleft()\r\n # Converting deque to list\r\n givenlist = list(dequelist)\r\n # return the result list\r\n return givenlist\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"Hello\", \"Btech\", \"Geeks\", \"is\", \"new\", \"online\"]\r\n# passing list to remove first element\r\nprint(removeFirstElement(givenlist))\r\n<\/pre>\n
['Btech', 'Geeks', 'is', 'new', 'online']<\/pre>\n
\n