Play with Python list – Part 1


Hey Folks,

I hope you are doing well. Today we are going to learn about the Python list, which is very simple but sometimes quite confusing so let’s get started.

First let’s start with creating a list.

data = ['a', 'b', 'c']

So now we have created a simple list so, we will use this list throughout the blog.

Now we will see some of the most popular methods which can help us in our day-to-day programming.

  1. index()
data.index('b')

Output,

1

So here we get index 1, as the index always starts with 0.

2. append()
Append method will add an item at the end of the list.

data.append('d')
print(data)

So we will get the bellow output.

['a', 'b', 'c', 'd']

We can also append a list to a different list,

data.append(['e', 'f'])
print(data)

Output,

['a', 'b', 'c', 'd', ['e', 'f']]

3. extend()
Extend method will extend the list and add data at the end of the list.

data.extend('g')
print(data)

Output,

['a', 'b', 'c', 'd', ['e', 'f'], 'g']

Now you will ask if extend add data at the end of the list and append also doing the same thing then, what is the difference between them? So let’s understand this. Let’s try to add a list with extend method,

data.extend(['g','h'])
print(data)

Output,

['a', 'b', 'c', 'd', ['e', 'f'], 'g', 'g', 'h']

So as you see, when we add a list with the append method, it appends the list as it was, and when we add a list with the extend method, it adds all elements individually. So that is the difference between append() and extend().

4.insert()
If you want to add an item in a specific position you can use the insert method. Insert method will take 2 parameters one is index number and another is element or item you want to insert.

Now we had done too many operations on the previous list, so let’s start with a new list, so for that, we need to initialize a new list with data.

data = ['a', 'c', 'd']
data.insert(1, 'b')
print(data)

Output,

['a', 'b', 'c', 'd']

We can also insert an another list to a list, let’s see the example,

data.insert(3, ['test', 'test1'])
print(data)

Output,

['a', 'b', 'c', ['test', 'test1'], 'd']

5.remove()
Remove method will remove a specific element or item.

data.remove('d')
print(data)

Output,

['a', 'b', 'c', ['test', 'test1']]

you can also remove multiple elements, check below example.

data.remove(['test', 'test1'])
print(data)

Output,

['a', 'b', 'c']

6. count()
Count method will count a specific element occurs how many time.

data = ['a','b', 'c', 'd', 'b']
data.count('b')

Output,

2

you can also count multiple elements, it will count in the sequence.

data = ['a','b', 'c', 'a','b', 'd', 'b']
data.count(['a', 'b'])

Output,

0

Now you will think about why the output is 0 as there are elements ‘a’ and ‘b’ which are in sequence, So let’s check with the list added inside the list, this way you will get to answer your question.

data = [['a','b'], 'c',['a','b'], 'd', 'b']
data.count(['a', 'b'])

Output,

2

And the count increases!

I hope you get a brief idea about all these methods. Let us know in the comment section if you have any queries related to any of these methods.

Part 2 of this blog will be posted soon! To get notifications of a new blog to follow us or you can directly follow us via your email, just enter your email id in the ‘FOLLOW BLOG VIA EMAIL’ section, you can find this in the footer section at the end of the page, and don’t forget to rate this blog!

Thank you for reading 🙂
Happy Coding 🙂

2 thoughts on “Play with Python list – Part 1

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started