Play with Python list – part 2


Hello folks,

I hope you all are healthy and safe at your home. As promised, I’m back with part 2 of the Python list blog. If you haven’t read part – 1, you can read it from here ‘Play with Python list – Part 1‘. So, now let’s dive into part 2 and learn some more methods.

Let’s create a list on which we can play around.

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

1. pop()
The pop method takes one parameter, which is an index. The pop method will remove an item of a given index.

data.pop(1)
print(data)

Output,

['a', 'c']

We can also use pop method without passing index. In that case it will remove last element or the item of the list. Let’s see the example,

data = ['a', 'b', 'c']
data.pop()
print(data)

Output,

['a', 'b']

2. reverse()
The reverse method, as the name says it will reverse your whole list.

data = ['a', 'b', 'c']
data.reverse()
print(data)

Output,

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

3. sort()
The sort method will sort your list in ascending or descending order. This method will allow two parameters which are the key and reverse. For parameter keys, you use one user-defined or default function. And for parameter reverse, you can assign a value either ‘True’ or ‘False’ True will reverse your list in descending order which is also a default one, and False will convert the list into an ascending order sequence.

data = ['b', 'a', 'c']
data.sort(reverse=True)
print(data)

Output,

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

Another example,

data = ['b', 'a', 'c']
data.sort(reverse=False)
print(data)

Output,

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

4. sorted()
The sorted method is also a bit similar to the sort method. The difference is in syntax and behavior. The main difference between the sort method and the sorted method is, the sort method will change the actual list, whereas the sorted method will not change the actual list and, it will just return the sorted list.

data = ['b', 'a', 'c']
sorted(data)

Output,

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

As you can see, I didn’t write the print method to print the output. If I’ll write the print(data) then it will just display [‘b’, ‘a’, ‘c’] as an output.
5. copy()
The copy method will copy one list into another list. We can copy list using ‘=’ operator or using copy method. First let us check exapmle with ‘=’ operator.

data = ['a', 'b', 'c']
copyData = data
print(copyData)

Output,

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

This new list will be going to refer to the original list. So when you make any change to copied list it will also going to change the original list. Let’s see an example.

copyData.append('d')
print('copyData: ', copyData)
print('data: ', data)

Output,

copyData: ['a', 'b', 'c', 'd'] 
data: ['a', 'b', 'c', 'd']

To overcome this issue we can use the copy method. copy method did not take any parameters.

data = ['a', 'b', 'c']
copyData = data.copy()
print(copyData)

Output,

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

Now we will modify this new list and check the output.

copyData.append('d')
print('copyData: ', copyData)
print('data: ', data)

Output,

copyData: ['a', 'b', 'c', 'd'] 
data: ['a', 'b', 'c']

I hope you got the difference between the two, so let’s move forward.
6. clear()
The last method is clear. The clear method will clear all items from the list.

data = ['a', 'b', 'c']
print(data)
data.clear()
print(data)

Output,

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

If you wish to know any other methods, let me know in the comment section, I’ll write part – 3 of this blog. I hope each method is clear to you. If you have any doubts, you can write them down in the comment section.

Thank you for reading 🙂
Happy coding 🙂

One thought on “Play with Python list – part 2

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started