Python List

Python list is used to store data of different types,if you have done array you know that it is used to store data of similar types but only python gives you the flexibility of storing different data types under a single common name.

Things theoretically seems bit different and complex but by a single example it will become more clear to you all.

list in python

In this example we have stored different strings and numeric value inside names,in this way we can store different types of data in list.

Features of Python List

  • Python list is a data structure which is used to store various types of data.
  • In Python, lists are mutable i.e., Python will not create a new list if we modify an element of the list.
  • It works as a container that holds other objects in a given order. We can perform various operations like insertion and deletion on list.
  • A list can be composed by storing a sequence of different type of values separated by commas.
  • Python list is enclosed between square[] brackets and elements are stored in the index basis with starting index 0.

In the exapmle you can also see that elements (i.e. values/data) of list can be called by there index numbers as in string we can get characters similarly you can here work too.

Python list

In this example we can see that e have a lot’s of predefined methods in python for list to work on and when you type any list name and then apply . (dot sign)a popup comes automatically showing you lot’s of suggestion you can use according to your desired.

Python List Syntax

 

    =[value1,value2,value3,…,valuen];  

Syntax to Access Python List

 

       [index]  

Python List Operations

Apart from creating and accessing elements from the list, Python allows us to perform various other operations on the list. Some common operations are given below:

a) Adding Python Lists

In Python, lists can be added by using the concatenation operator(+) to join two lists.

Add lists Example 1

Python list operations

Note: ‘+’operator implies that both the operands passed must be list else error will be shown.

b) Python Replicating lists

Replicating means repeating, It can be performed by using ‘*’ operator by a specific number of time.

Python list Replication Example

Python List

Python List Other Operations

Apart from above operations various other functions can also be performed on List such as Updating, Appending and Deleting elements from a List.

Python lists Method

Python provides various Built-in functions and methods for Lists that we can apply on the list.

Following are the common list functions.

FunctionDescription
min(list)It returns the minimum value from the list given.
max(list)It returns the largest value from the given list.
len(list)It returns number of elements in a list.
cmp(list1,list2)It compares the two list.
list(sequence)It takes sequence types and converts them to lists.

Python List min() method Example

This method is used to get min value from the list.

  

Python List max() method Example

This method is used to get max value from the list.

 

Python List len() method Example

This method is used to get length of the the list.

Example of min,max,len is here:

Operations in Python List

There are following built-in methods of List

MethodsDescription
index(object)It returns the index value of the object.
count(object)It returns the number of times an object is repeated in list.
pop()/pop(index)It returns the last object or the specified indexed object. It removes the popped object.
insert(index,object)It inserts an object at the given index.
extend(sequence)It adds the sequence to existing list.
remove(object)It removes the object from the given List.
reverse()It reverses the position of all the elements of a list.
sort()It is used to sort the elements of the List.

try all the Methods yourself and then  let us discus,hopefully you are enjoying the series 😉👍

Leave a Reply