DEV Community

Cover image for Learn Python #7 - List operations 💡!
Nitin Reddy
Nitin Reddy

Posted on

Learn Python #7 - List operations 💡!

💡This is a continuation of what we had learnt in our last post. Let us learn more about some other operations that could be performed on the python lists.

📘So today we will be covering the following list of operations:-

  • Combining list
  • Removing list items
  • Clearing out a list
  • Counting how many times an item appeared in the list
  • Finding a list item's index

Combining list

In python, combining the two lists is bit easy. Let's learn through an example...

list1 = ['Roger', 'Stark', 'Banner'] //List 1
list2 = ['Steve', 'Tony', 'Bruce'] //List 2

list1.extend(list2)
//['Roger', 'Stark', 'Banner', 'Steve', 'Tony', 'Bruce']
Enter fullscreen mode Exit fullscreen mode

In the above example, list1 is extended by including the list items of list2.

Removing list items

There are different ways in which the item can be removed from the list. Let's look at them one by one

  • remove method
list1 = ['Roger', 'Stark', 'Banner']
list1.remove('Roger')
Enter fullscreen mode Exit fullscreen mode
  • pop method
list1 = ['Roger', 'Stark', 'Banner', 'Steve', 'Tony', 'Bruce']
list1.pop(0) // 'Roger' is removed from the list since the index specified is 0
// Result - ['Stark', 'Banner', 'Steve', 'Tony', 'Bruce']

list1.pop() // 'Bruce' is removed from the list as it was the last item on the list.
// Result - ['Stark', 'Banner', 'Steve', 'Tony']
Enter fullscreen mode Exit fullscreen mode
  • del keyword
list1 =  ['Roger', 'Stark', 'Banner', 'Steve', 'Tony', 'Bruce']
del list1[2]
// Result -  ['Roger', 'Stark', 'Steve', 'Tony', 'Bruce']
Enter fullscreen mode Exit fullscreen mode

Clearing out the list

In case you want the delete the content of the lists but not the list, you can use the clear method on the list.

list1 =  ['Roger', 'Stark', 'Banner', 'Steve', 'Tony', 'Bruce']
list1.clear()
// Result - []
Enter fullscreen mode Exit fullscreen mode

Easy peasy isn't it?

Counting how many times an item appeared in the list

You can also find the number of appearances of a specific item in a list. This can be achieved by using the count method on a list that takes an item as an argument.

list1 =  ['Roger', 'Stark', 'Banner', 'Roger', 'Tony', 'Bruce']
list1.count('Roger')
// Result - 2
Enter fullscreen mode Exit fullscreen mode

Finding a list item's index

This operation would be pretty useful when you want to find and replace an item in the list with another item.
To find an index of an item in a list, we use the index method.

list1 =  ['Roger', 'Stark', 'Banner', 'Roger', 'Tony', 'Bruce']
list1.index('Roger')
Enter fullscreen mode Exit fullscreen mode

The catch here is that this method will return the index of the first instance of an item from the list.

Conclusion

There are more posts on the way related to the lists...So keep reading.

Thanks in advance for reading this article...🚀

I am more than happy to connect with you on

You can also find me on

Top comments (0)