10 most useful list methods in Python



Python provides a wide range of built-in methods for working with lists, making it easy to manipulate and transform data. In this tutorial, we will take a look at some of the most useful list methods in Python.



    1. list.append(item): This method is used to add an item to the end of a list. For example, numbers = [1, 2, 3]; numbers.append(4) will add the number 4 to the end of the list. So, numbers will be [1, 2, 3, 4].
    2. list.extend(iterable): This method is used to add multiple items to the end of a list. The argument passed to the method should be an iterable (e.g. a list or a tuple). For example, numbers = [1, 2, 3]; numbers.extend([4, 5, 6]) will add the numbers 4, 5, and 6 to the end of the list. This is also possible to do with numbers += [4, 5, 6]. In both cases, the list numbers will have [1, 2, 3, 4, 5, 6].
    3. list.insert(index, item): This method is used to insert an item at a specific index in a list. For example, numbers = [1, 2, 3]; numbers.insert(1, 4) will insert the number 4 at index 1 in the list. So, the list will have [1, 4, 2, 3].
    4. list.remove(item): This method is used to remove the first occurrence of a specific item from a list. For example, numbers = [1, 2, 3, 4, 4]; numbers.remove(4) will remove the first occurrence of the number 4 from the list. In this case, the list numbers will become [1, 2, 3, 4].
    5. list.pop(index): This method is used to remove an item at a specific index in a list and return the removed item. For example, numbers = [1, 2, 3]; numbers.pop(1) will remove the item at index 1 (2) and return it. In case no index is specified, the pop function removes the last element from the list. In our example, the list numbers after performing pop(1) will become [1, 3].
    6. list.index(item): This method is used to find the index of the first occurrence of a specific item in a list. In case of numbers = [1, 2, 3, 4, 4]; numbers.index(4) will return the index of the first occurrence of the number 4 in the list. In this case the returned index will be 3.
    7. list.count(item): This method is used to count the number of occurrences of a specific item in a list. For example, numbers = [1, 2, 3, 4, 4]; numbers.count(4) will return the number of occurrences of the number 4 in the list — which is 2 in this example.
  1. list.sort(): This method is used to sort the items in a list in ascending order. For example, numbers = [3, 1, 4, 2]; numbers.sort() will sort the list in ascending order and the list will become [1, 2, 3, 4].
  2. list.reverse(): This method is used to reverse the order of the items in a list. For example, numbers = [1, 2, 3]; numbers.reverse() will reverse the order of the items in the list. So, the numbers list will turn into [3, 2, 1].
  3. list.clear(): This method is used to remove all items from a list, effectively emptying it. For example, numbers = [1, 2, 3]; numbers.clear() will remove all items from the list and make it an empty list.
    You can also use del keyword to remove all items in a list but the difference is that del keyword is used to delete the list variable entirely from the memory while list.clear() only empties the list.
numbers = [1, 2, 3]
del numbers
# numbers variable doesn't exist anymore

numbers = [1, 2, 3]
numbers.clear()
print(numbers) # []
See also  THE PYTHON BIBLE 7 IN 1

Hot Topics

Related Articles

bitcoin
Bitcoin (BTC) $ 118,230.68
ethereum
Ethereum (ETH) $ 3,779.51
tether
Tether (USDT) $ 1.00
bnb
BNB (BNB) $ 795.30
xrp
XRP (XRP) $ 3.19
cardano
Cardano (ADA) $ 0.82742
usd-coin
USDC (USDC) $ 1.00
matic-network
Polygon (MATIC) $ 0.236921
binance-usd
BUSD (BUSD) $ 0.997838
dogecoin
Dogecoin (DOGE) $ 0.239052
okb
OKB (OKB) $ 48.29
polkadot
Polkadot (DOT) $ 4.17
shiba-inu
Shiba Inu (SHIB) $ 0.000014
tron
TRON (TRX) $ 0.320711
uniswap
Uniswap (UNI) $ 10.61
wrapped-bitcoin
Wrapped Bitcoin (WBTC) $ 118,056.63
dai
Dai (DAI) $ 1.00
litecoin
Litecoin (LTC) $ 114.68
staked-ether
Lido Staked Ether (STETH) $ 3,769.86
solana
Solana (SOL) $ 186.92
avalanche-2
Avalanche (AVAX) $ 24.95
chainlink
Chainlink (LINK) $ 18.68
cosmos
Cosmos Hub (ATOM) $ 4.78
the-open-network
Toncoin (TON) $ 3.33
ethereum-classic
Ethereum Classic (ETC) $ 23.14
leo-token
LEO Token (LEO) $ 8.98
filecoin
Filecoin (FIL) $ 2.69
bitcoin-cash
Bitcoin Cash (BCH) $ 567.40
monero
Monero (XMR) $ 322.36