Lists – Python

Hello Techies!!!

Python or any other language gives us a way to store data in a structured way, so that, we can maintain integrity and sanctity of the code.

Python Lists is one of the way to do the same.

What are lists? What is its use?

List is one of the many ways to store our data in the code. It can be used to store data, so that we can later manipulate or use it according to our wish.

It is a collection of data which is ordered and changeable. It also allows to store duplicate data.

In other languages, you can find list named as arrays. Basic difference between other programming languages and python is that in python list can store any type of data together but in others we need to define data type of the array. Arrays are meant to only store data of same data type.

In python, we get flexibility to store any type of data into list.

Let’s make a list of colors :

colors = [“red”, “green”, “yellow”, “blue”, “black”, “white”]

This is how you write a list in python. We can say that we have a list of colors which is named ‘colors’.

Square brackets [ ] tells us that colors is of list type.

we can also define a mixed data type list:

mixed_list = [“red”, 1, 12.4, “yellow”, “white”]

DEALING WITH LIST :

ACCESS, CHANGE AND ITERATE ELEMENT FROM LIST:

list-1.JPG

We can access the element similar to the way we accessed character of a string by passing index ( which starts from 0 ) into square bracket in front of list name.

We can change element of list by mentioning index and using assignment operator to assign new value. This makes list mutable ( changeable ).

We can use for loop for iterating through a list.

Above, we are taking each color and assigning it to variable ‘color’ and printing it on the console.

LENGTH OF LIST, CHECKING IF ITEM EXISTS IN LIST, ADDING, DELETING AN ELEMENT :

list-2.JPG

Here, we used a bunch of built-in functions,

len() – To find the length of the list

append() – To add element at the end of the list

insert() – To add element at specific index

pop() – To remove element from specified index and if not mentioned remove element from the end of the list

remove() – To remove a mentioned element.

list-3.JPG

We can also use,

clear() – To clear all the elements from the list

del – Keyword to delete the list all together from memory or to delete specified index element

NOTE : 

NameError : This is an error thrown by compiler when we try to use a variable that is not defined or does not exist.

So, this is all about list. We will learn about more ways to store different forms of data into different collections.

See you guys in next tutorial. Have a great day!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.