Python list methods

let's play with lists using Python.

Python list is used for store many values in a single variable. It can store integers, strings, and also booleans in a single list.

Stuff Python list can store

  • Numbers (int and float)
  • Strings (str)
  • Booleans (bool)
  • You can also store lists!
  • Dictionaries

Let's define our list...

# defining new fruits list
fruits = ["apple", "banana", "cherry", "lemon"]

# print the fruit list
print(fruits)
# Output
["apple", "banana", "cherry", "lemon"]

... and also access them.

# get fruit first element (index 0)
print(fruits[0])

# get fruits third element (index 2)
print(fruits[2])
# Output
"apple"  # index 0
"cherry" # index 2

There are several list methods can be used.

  • .append() - adds an element in the end of the list.
  • .clear() - makes the list empty - clear all elements.
  • .copy() - return the copy of the list.
  • .count() - get the numbers of list elements containing.
  • .extend() - add a list of elements at the end of the list.
  • .index() - get the index of first matched element.
  • .insert() - add the element in specific index.
  • .pop() - remove element in specific index.
  • .remove() - remove element in certain value.
  • .reverse() - reverse the list order.
  • .sort() - sort the list elements in ascending order.

list.append(x)

Adds an element x in the end of the list.

Let's go back into fruits list.

fruits = ["apple", "banana", "cherry", "lemon"]

Then add an element using .append().

fruits.append("kiwi")
print(fruits)  # outputs ["apple", "banana", "cherry", "lemon", "kiwi"]

list.clear()

Remove all items from the list.

fruits = ["apple", "banana", "cherry", "lemon"]

fruits.clear()

print(fruits) # outputs []

list.copy()

Return a "shallow copy" of the list.

fruits = ["apple", "banana", "cherry", "lemon"]

my_fruits = fruits.copy()

print(my_fruits)
# outputs
# ["apple", "banana", "cherry", "lemon"]

list.count(x)

Return the number of x times appeared in the list.

fruits = ["apple", "banana", "cherry", "lemon"]

my_fruits = fruits.copy()

print(my_fruits.count("apple")) # outputs 1
                                # bcoz' there is 1 "apple"

list.extend(iterables)

Extend the list by appending all the items from the iterable (lists, dictionaries, sets, etc.).

fruits = ["apple", "banana", "cherry", "lemon"]
another_fruits = ["grape", "papaya", "watermelon"]

# append another_fruits list
## into fruits
fruits.extend(another_fruits)


print(fruits) # outputs ['apple', 'banana', 'cherry', 'lemon',
              #          'grape', 'papaya', 'watermelon']

list.index(x, [start, [end]])

Return zero-based index in the list of the first item whose value is equal to x.

fruits = ["apple", "banana", "cherry", "lemon"]

# find index of "lemon"
print(fruits.index("lemon")) # outputs 3

list.insert(i, x)

Insert an item x at a given position i.

fruits = ["apple", "banana", "cherry", "lemon"]

# insert "blueberry" on index 2
fruits.insert(2, "blueberry")
print(fruits) 

# outputs
# ['apple', 'banana', 'blueberry', 'cherry', 'lemon']

list.pop([i])

Remove the item at the given position i in the list.

fruits = ["apple", "banana", "cherry", "lemon"]

# remove item in index 1
fruits.pop(1)
print(fruits) 

# outputs
# ['apple', 'cherry', 'lemon']

list.remove(x)

Remove the first item from the list whose value is equal to x.

fruits = ["apple", "banana", "cherry", "lemon"]

# remove "apple"
fruits.remove("apple")
print(fruits) 

# outputs
# ['banana', 'cherry', 'lemon']

list.reverse()

Reverse the elements of the list in place.

fruits = ["apple", "banana", "cherry", "lemon"]

# reverse fruits order
fruits.reverse()
print(fruits) 

# outputs
# ['lemon', 'cherry', 'banana', 'apple']

list.sort(key=None, reverse=False)

Sort the items of the list in place.

fruits = ["apple", "cherry", "lemon", "banana"]

# sort fruits order
fruits.sort()
print(fruits) 

# outputs
# ['apple', 'banana', 'cherry', 'lemon']

Some reference(s)

Thank you