JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Remove the first element from a list in Python

Author:JIYIK Last Updated:2025/05/05 Views:

This tutorial will discuss different ways on how to remove the first element from a list.


pop()Remove the first element from a list using the

pop()Method can remove an element from a specific index. We have to specify the index from which the element is to be removed.

In our case, we have to remove the first element, so we have to use indexing 0.

For example,

list1 = ["ram", "ravi", "shyaam"]
list1.pop(0)
print(list1)

Output:

['ravi','shyaam']

If index is not specified, removes the last element.


remove()Remove the first element from a list using the

removemethod can remove any required element from the list. Here, we have to write the name of the element to be removed instead of its index.

In our case, we will write the first element of the list.

For example,

list1 = ["ram", "ravi", "shyaam"]
list1.remove("ram")
print(list1)

Output:

['ravi','shyaam']

Assume that we don't know the first element and checking again and again may be time consuming. To avoid this, we can also use remove()method in the following way.

list1 = ["ram", "ravi", "shyaam"]
list1.remove(list1[0])
print(list1)

Output:

['ravi','shyaam']

delDeleting elements from a list using keyword in Python

delThe keyword also removes the element from a specific index. We will write 0 in the brackets as it specifies the first element in the list.

For example,

list1 = ["ram", "ravi", "shyaam"]
del list1[0]
print(list1)

Output:

['ravi','shyaam']

Remove the first element from a list using list slicing in Python

We can also use the slice method to remove the first element. This method is the most common way programmers solve this problem.

Here, we specify the starting element we want the list to have and the desired last value. In our case, we will start at index 1 and end at n-1 by removing the first element.

Please refer to the code below.

list1 = ["ram", "ravi", "shyaam"]
list1 = list1[1:]
print(list1)

Output:

['ravi','shyaam']

Use numpy.delete()the function to remove the first element from a list in Python

We can use the function NumPyfrom the module delete(). First, we numpy.array()convert the list to an array using the function and then delete()remove the required elements using the method.

For example,

import numpy as np

list1 = ["ram", "ravi", "shyaam"]
arr = np.array(list1)
arr = np.delete(arr, 0)
print(arr)

Output:

['ravi','shyaam']

popleft()Remove the first element from a list using the function in Python

popleft()The function removes the elements one by one from the beginning. But first, we convert the list into a deque and then use this method. After the required transformation, we reverse the deque back into a list and then print the output.

The module needs to be imported before collectionsthis method can be used.

Please refer to the code below.

import collections

list1 = ["ram", "ravi", "shyaam"]
# convert list to deque
deq = collections.deque(list1)
# removing from left side
deq.popleft()
# convert deque back to list
list1 = list(deq)
print(list1)

Output:

['ravi', 'shyaam']

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Remove all occurrences of an element from a Python list

Publish Date:2025/05/08 Views:69 Category:Python

In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element

Convert hex to bytes in Python

Publish Date:2025/05/08 Views:101 Category:Python

Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for

b in front of string in Python

Publish Date:2025/05/08 Views:53 Category:Python

This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v

How to Convert Integer to Binary in Python

Publish Date:2025/05/08 Views:130 Category:Python

This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,

How to convert an integer to bytes

Publish Date:2025/05/08 Views:77 Category:Python

Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in

How to convert bytes to int in Python

Publish Date:2025/05/08 Views:111 Category:Python

Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho

How to Remove Punctuation from a String in Python

Publish Date:2025/05/08 Views:190 Category:Python

This tutorial discussed methods for removing punctuation from strings in Python. This is a particularly useful step when preprocessing and cleaning text data for NLP. string Remove punctuation from a string in Python using class methods We

How to Convert an Integer to a String in Python

Publish Date:2025/05/08 Views:69 Category:Python

This article will show you different ways to convert an integer to a string using Python code, such as str() the function and the f format method. str() Convert integer to string in Python using function We can use the inbuilt function in P

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial