5 Methods to use with Python Lists!

5 Methods to use with Python Lists!

·

3 min read

An important data structure that Python users regularly use in their code, is the list data type. Lists are the equivalent of arrays for Python.

Functionally, they are one of the most used data structures. Knowing a few methods could make your code a little more sophisticated and make life a lot easier!

1) list.append(x)

This method is a popular way to add more items to a list that is pre-existing. Sample code for this looks something like this:

sampleList = [item1, item2, item3]
sampleList.append(item4)

And the new list would look something like this (after you print the list, of course!):

[item1, item2, item3, item4]

2) list.insert(i,x)

This method, as suggested by the name, is used to place an item x at a location before i within a pre-existing list. The i here is the index of the element before which the x is placed.

Code for this looks something like this:

sampleList = [item1, item2, item3]
sampleList.insert(2, item4)

And the new list after the addition of the new item would look like this:

[item1, item2, item4, item3]

3) list.index(x, *start, *end)

This method is used to return the location of item x in a pre-existing list, aka, the index. Other parameters you can include with this method are - declaring the starting and ending indices of the list, between which you want the search of the index of item x to take place.

Code for this looks like this:

sampleList = [item1, item2, item3]
sampleList.index(item1)

Printing the result of the above method would return:

0

Another sample code using the parameters:

sampleList = [item1, item2, item1, item2, item1, item2]
sampleList.index(item2, 1, 4)

The result of printing the above code would give you the position of item1 between positions 1 to 4:

2

4)list.count(x)

This method returns the number of times an item x occurs in a pre-existing list.

A good example of this is:

sampleList = [item1, item2, item1, item2, item1, item2] 
sampleList.count(item1)

The result of printing the method would be:

3

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

This method returns a sorted version of a pre-existing list, in ascending order by default.

sampleList = [2, 3, 1, 4]
sampleList.sort()

Printing the above would result in the sampleList looking like this:

[1, 2, 3, 4]

If parameter reverse is changed to 'True', the same list will be returned in descending order.

sampleList.sort(reverse=True)

Would result in the sampleList being arranged in the following way:

[4, 3, 2, 1]

If you want the sorting to take place in a custom manner, the key function can be used as a parameter. One example is shown below:

sampleList = ['and', 'I', 'am']
sampleList.sort(key=len)

Would result in the sampleList being sorted in the order of increasing lengths of the strings. This is shown below:

['I', 'am', 'and']

Hopefully, this covers some of the most important methods in lists. Make sure to test these out in your code editors before use, as implementing always helps you understand syntax better. Python offers a few more methods in the official documentation with great examples to follow. It is worth checking out!