• Srujan kachhwaha

Python Lists And List Built-in Fuctions, Your Flexible And Dynamic Data-Structure

Updated: Jul 25

In the last post, we read about how choosing a good data structure is important in programming.

In this post, we will talk about the List Data Structure available in python and some built-in functions available in the list that comes with python.


Initialize a List In Python


You can initialize a list in many ways :

Initialize an empty list

>>> l = []

Or

>>> l = list()

Or Initialize a list with some predefined variables:

>>> l = [1,2,3,4]

The best part of lists in python is that it can work with any data type you feed it. For example

>>> l = [1, 1.5, 'hello']
>>> l
[1, 1.5, 'hello']


Accessing Elements Of The List


Accessing elements is easy, you just need to give it the index number of the element you want.

>>> l[2]
'hello'

Nested List In Python


We can also do nested lists in python, i.e lists inside a list, Let's look at an example:

>>>l = [[1,2,3], ["hey","iam","learning",34,56], [2,3,4]]

Accessing Elements Of Nested List


This is easy and a little tricky but let's explore it. In Our above list what if I do :

>>>l[1]
["hey","iam","learning",34,56]

See! but why? When you look at it, it is like we have a list l and in that list at position index 1 we have another list.


But what if I want to access "learning"? "learning" is in the other list which is at position 1 inside the list l. So, first, we try to Access the list which contains the element "learning" and then we will access our desired element.

>>>l[1][2]
"learning"

Simple. You can play with the nested list until you get comfortable with them.


Built-in Function Available In List


We have some built-in functions which help us to play and manipulate lists without any hustle. Let's talk about them and see them in action.

  • append

  • copy

  • extend

  • insert

  • remove

  • sort

  • clear

  • count

  • index

  • pop

  • reverse

append in python


Let us suppose you add some elements to your list as

>>> l = [1,2,"hello"]

But later, you decide to add some more elements. How will you do it? Lists in python are flexible and scalable at any time.

Use the append method to insert the element at the end of the list.


>>> l
[1,2,"hello"]
>>> l.append(5)
>>>l
[1,2,3,"hello",5]
>>> l.append("list is cool")
>>> l 
[1,2,3,"hello",5,"list is cool"]

insert in python


What if you want to insert an element at a specific position? Let's try to insert "soul" at index 0.

>>>l.insert(0,"soul")
>>>l
["soul",1,2,3,"hello",5,"list is cool"]

Easy right! all the elements shift away just like that. If you were in java or c++ you need to write a logic to do that.


count in python


What if you want to determine how many times an item is repeated inside a list? Remember that question where you need to determine the frequency of an item inside a list or array!

In Python, we do it with a single line of code


>>> l.count("soul")
1

Finding frequency problem got away just like that. woosh.


index in python


Use index function to know the index of an element, this comes in handy often while working with huge data. We don't know where the element in dumped inside the list


>>> l.index("soul")
0

sort in python


Sorting! There are many ways to do sorting, but what's easier than to call just a function!


>>> l = [9,3,2,8,1]
>>> l.sort()
>>> l
[1,2,3,8,9]

What we have a list with strings? Does it work?

>>> l = ['a','z','b','k']
>>>l.sort()
>>>l
['a','b','k','z']

See! Python can handle anything


reverse in python


I think you got an idea by the name of what this function does!

>>>l.reverse()
>>>l
['z','k','b','a']

pop in python


pop will simply pop out the item at the top of the list

>>> l.pop()
'z'

Let's wind up this post here. For now, keep playing with the concepts you got. Will meet in the next post.


9 views0 comments