DEV Community

Cover image for Lists in python and In-build Functions
KetanIP
KetanIP

Posted on • Edited on • Originally published at ketaniralepatil.com

Lists in python and In-build Functions

Today we will together see what are lists and some of the most used in-built python function for lists. Without wasting our time let's start.

What Are Lists ?

In python list are a collection of objects which are indexed. A list can can have any kind of data in it which incudes String, Integer, Boolean, Dictionaries, even liste ( i.e. nested lists ) and so on.

Here is an example of list.

randomList = ["A String", 1, 2.0, True, {"obj":"value"}, [1, 'more', 'list']]
Enter fullscreen mode Exit fullscreen mode

This is not an ideal list but it servers the purpose of teaching about lists showing that list can contain any kind of data. Most of the times you need to and you must use a single data type per list.

Inbuild List Functions

There are many in-build list functions, most used of them are :

  1. Append
  2. Extend
  3. Insert
  4. Remove
  5. Pop
  6. Clear
  7. Index
  8. Count
  9. Sort

A detail explanation of each of the function is as follows.

Append - append()

Append is used to add / push object at the end of the list. If you want to push to an priticular index in list then you must use insert method.

fruits = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"]
fruits.append("Orange")
print(fruits)

# Response -> ["Apple", "Banana", "Mangoes", "Grapes", "Kiwi", "Orange"]
Enter fullscreen mode Exit fullscreen mode

Extend - extend(list_name)

Extend is used to add list or literable object at the end of the list. In the example below I have joined two lists.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"]
moreFruits = ["Papaya", "Dragon Fruit"] 
fruits.extend(moreFruits)
print(fruits)

# Response -> ["Apple", "Banana", "Mangoes", "Grapes", "Kiwi", "Papaya", "Dragon Fruit"]
Enter fullscreen mode Exit fullscreen mode

Insert - insert(index, "Object")

Insert is used to add object in the list at a given index. In the example below I have added Orange in the list at second place.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"] 
fruits.insert(1, "Orange")
print(fruits)

# Response -> ["Apple", "Banana", "Mangoes", "Grapes", "Kiwi", "Orange"]
Enter fullscreen mode Exit fullscreen mode

Remove - remove(index)

Remove is used to add object in the list at a given index. In the example below I have removed Banana in the list from index 1.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"] 
fruits.remove(1)
print(fruits)

# Response -> ["Apple","Mangoes", "Grapes", "Kiwi"]
Enter fullscreen mode Exit fullscreen mode

Pop -pop([index])

Pop is used to remove object at the end of the list, it has an optional index value ( integer) use to remove object at that index like remove method. In this example we will remove Kiwi from fruits.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"] 
fruits.pop()
print(fruits)

# Response -> ["Apple", "Banana", "Mangoes", "Grapes"]
Enter fullscreen mode Exit fullscreen mode

Note : [index] is an integer value [] are use to represent it as optional parameter.

Clear - clear()

Clear is used to remove all elements from the list. In this example we will empty fruits list.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"] 
fruits.clear()
print(fruits)

# Response -> []
Enter fullscreen mode Exit fullscreen mode

Index - index(Object)

Index is used to find the index of the given object in the list, it will only return one single result from a list even it has many copies of the same object. In this example we will find index of Banana.

fruits     = ["Apple", "Banana", "Mangoes", "Grapes","Kiwi"] 
fruits.index("Banana")
print(fruits)

# Response -> 1
Enter fullscreen mode Exit fullscreen mode

Let me show you what happens is there are multiple copies of same object in a given list and when it is used in index method.

fruits     = ["Apple", "Banana", "Banana", "Mangoes", "Banana" "Grapes", "Banana", "Banana", "Banana", "Kiwi"] 
fruits.index("Banana")
print(fruits)

# Response -> 1
Enter fullscreen mode Exit fullscreen mode

This will still return 1 as when the function will find the object it will stop searching and will return result as the index of that object, here "Banana".

Count - count(Object)

Count counts the number of times the object has appeared in the list. In this example we will use count function on "Apple" and see how many times it has appeared in the list. The "Apple" is at two index ( 0 and 2 ) so the value returned by count method will be 2.

fruits     = ["Apple", "Banana", "Apple", "Mangoes", "Grapes","Kiwi"] 
print(fruits.count("Apple"))

# Response -> 2
Enter fullscreen mode Exit fullscreen mode

Sort - sort([reverse=False])

As the name suggests sort function will sort the list either in numerical order ( from 0 to 9 ) or in alphabetical order as if one uses both than it will throw an error.It has optional revrse parameter as the name suggests it reverses the sorted list. Now lets see this in action.

fruits     = ["Apple", "Banana", "Apple", "Mangoes", "Grapes","Kiwi"] 
fruits.sort()
print(fruits)

# Response -> ['Apple', 'Apple', 'Banana', 'Grapes', 'Kiwi', 'Mangoes']
Enter fullscreen mode Exit fullscreen mode

Hope you like this post. 😊
Thank You.

Guys follow me on Twitter and Instagram , it really helps.

Top comments (0)