Python List

PREV     NEXT

  1. Python list: List is a data structure which is a combination of homogeneous or heterogeneous data types
  2. Python list is similar like arrays in C/C++ Programming language
  3. We can use index to access the elements in list which starts from 0 and stops at n-1
  4. Elements are stored in sequence manner in the list
  5. Python List is mutable i.e. we can modify the elements in the list whenever we want
  6. Each element is separated by comma in the list
  7. We can create empty list also in python.

Different Types of Syntax:

Creating Empty List

list=[]

Note: It will create empty list

Create list with data:

list_name=[data1,data2,data3……….datan]

How print the complete list:

Example:

Compile & Run

Output:

[1, 2, 3, 4, 5]

How to access the list elements:

To access the elements in list, there are two  type of indexing available. They are,

  1. Forward Indexing
  2. Backward Indexing

1. Forward Indexing:

The starting index (mostly ‘0‘) is positive and ending index (n-1) is also positive, then we can call it as Forward Indexing i.e. almost it is in ascending order.

i.e.

Assume that a list is having following elements 1,2,3,4,5,6,7

my_list=[1,2,3,4,5,6,7]

In this case, start index is ‘0’ and end index is ‘6’

my_list[0] is 1

my_list[1] is 2

my_list[2] is 3

my_list[3] is 4

my_list[4] is 5

my_list[5] is 6

my_list[6] is 7

Below is the example for accessing the list elements for different data types.

Examples:

Compile & Run

Output:

list numbers
*************************
list_num[0]:10
list_num[1]:20
list_num[2]:30
list_num[3]:40
*************************
list float numbers
*************************
list_flt_nums[0]:1
list_flt_nums[1]:2
list_flt_nums[2]:3
list_flt_nums[3]:9
*************************
list characters
*************************
list_chars[0]:a
list_chars[1]:b
list_chars[2]:c
list_chars[3]:d

*************************
list strings
*************************
list_str[0]:fruits
list_str[1]:vegs
list_str[2]:raw
list_str[3]:drinks
*************************
list all
*************************
list_all[ 0 ]: 10
list_all[ 1 ]: 52.5
list_all[ 2 ]: fruits
list_all[ 3 ]: x
*************************

2. Backward Indexing:

The starting index ( mostly  ‘-n’ ) is negative number and ending index (-n+(n-1)) is also negative number, then we can call it as Backward Indexing i.e. Assume that the list is having following elements 1,2,3,4,5,6,7 i.e. here no. of elements are 7, so starting index (-n) means -7 and ending index means (-n+(n-1)) = -1

my_list=[1,2,3,4,5,6,7]

In this case start index is ‘-7’ and end index is ‘-1’

my_list[-7] is 1

my_list[-6] is 2

my_list[-5] is 3

my_list[-4] is 4

my_list[-3] is 5

my_list[-2] is 6

my_list[-1] is 7

Below is the example for accessing the list elements for different data types.

Example:

Compile & Run

Output:

list numbers
*************************
list_num[-4]:10
list_num[-3]:20
list_num[-2]:30
list_num[-1]:40
*************************
list float numbers
*************************
list_flt_nums[-4]:1
list_flt_nums[-3]:2
list_flt_nums[-2]:3
list_flt_nums[-1]:9
*************************
list characters
*************************
list_chars[-4]:a
list_chars[-3]:b
list_chars[-2]:c
list_chars[-1]:d

*************************
list strings
*************************
list_str[-4]:fruits
list_str[-3]:vegs
list_str[-2]:raw
list_str[-1]:drinks
*************************
list all
*************************
list_all[ -4 ]: 10
list_all[ -3 ]: 52.5
list_all[ -2 ]: fruits
list_all[ -1 ]: x
*************************

Note:

If we observe both above examples, here we can map “list_nums” as follows.

list_nums [0]=list_num[-4]=10

list_nums [1]=list_num[-3]=20

list_nums [2]=list_num[-2]=30

list_nums [3]=list_num[-1]=40

How Mutable the element in list:

In a list, we can change the data whenever we want. Please consider below example.

Example

Compile & Run

Output:

list_nums is
[1, 2, 3, 4, 5] list_nums after 1st iteration change
[1, 2, 3, 10, 5]
list_nums after 2nd iteration change
[7, 5, 3, 10, 5]

How to slice the element in list

List slice can be used to slice particular part of the list and as well as to modify the particular part of the list.

Example:

Compile & Run

Output:

[9, 6, 5, 8, 1, 3, 7, 4]
 [9, 6, 5]
[8, 1, 3, 7, 4]
[9, 6, 5, 8, 1]
[3, 7, 4]
[5, 8, 1]
[9, 6, 11, 12, 44, 3, 7, 4]

How to replicate the python list:

Here Replicate means adding the same list multiple time with all elements, we can achieve this mechanism by using asteristic symbol (‘*’)

Example:

Compile & Run

Output:

[1, 2, 3] [1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

How to add element, extend the  list, delete the list?

 Adding Two list:

 Example:

Compile & Run

Output:

[1, 2, 3, 4, 5, 6] 

We can add the elements in list by using multiple methods. They are,

  1. Append Method
  2. Insert Method

1.Append Method:

This method is used to add the elements at the end of the list.

Syntax:

append(value)

Value:

It will be any kind of data i.e. strings, nums

Return:

It will not return any value, it will modify the existing list only

Example:

Compile & Run

Output:

[‘fruits’, ‘veggies’, ‘dry’]
[‘fruits’, ‘veggies’, ‘dry’, 5]
[‘fruits’, ‘veggies’, ‘dry’, 5, ‘sports’]

2. Insert Method:

Insert method is used to add the element at given position or index.

Syntax:

insert(index,data)

index:

Given position or index , user choice of position or index

data:

The data which we want to insert at given index or position.

Example:

Compile & Run

Output:

[1, 2, ‘hi’, ‘hello’]
[1, 2, ‘hi’, 5.6, ‘hello’] [1, ‘start’, 2, ‘hi’, 5.6, ‘hello’]

Extend the list by using extend method in python:

Extend Method:

 This method is used to extend the already existing list.

Example:

Compile & Run

Output:

[1, 2, 3] [‘a’, ‘b’, ‘c’]
[1, 2, 3, ‘a’, ‘b’, ‘c’]

There are two methods in python to remove the requested element in the list by the user. They are,

  1. Remove Method
  2. Pop Method 

1. Remove Method

This method is used to remove the element at specific element

Syntax:

remove(element)

Example:

Compile & Run

Output:

[1, 2, ‘a’, ‘hg’, 4.5]
[1, 2[2, ‘hg’, 4.5], ‘hg’, 4.5]

2. Pop Method:

This method is divided into two types. They are,

  1. pop without arguments
  2. pop with argument

1. pop without arguments:

This method is used to remove the last element in the list.

Syntax:

list.pop()

Example:

Compile & Run

Output:

Last element is 4
[1,2, 3]

2. pop with argument:

This method is used to remove the element at specific index.

Syntax:

list.pop(index)

Example:

Compile & Run

Output:

1st element is 1
[2,3,4]

We can delete the list using two methods. They are,

  1. del method
  2. clear method
  3. del method:

We delete the complete list, bulk elements and single element  in python by using delete method.

Syntax:

del(index)  Note: Here index might be a single value, we can use slice of list also.

Example:

Compile & Run

Output:

[1, 2, 3, 4] [1.2, 2.5, 3.6, 4.3] [2, 3, 4] [] [3.6, 4.3] []

2. Clear method:

This method is used clear the complete list.

Syntax:

list_name.clear()

Example:

Compile & Run

Output:

[1, 2, 3, 4]
 []

Max, Min, Count, Len and Sum Methods:

Max Method:

          This Method is used to find the biggest element in the list.

Min Method:

          This Method is used to find the smallest element in the list.

Count Method:

          This Method is used to count the occurrence of specific element in the list.

Len Method:

          This Method is used to count the total elements in the list.

Sum Method:

          This Method is used find the sum of the elements in the list.

Example:

Compile & Run

Output:

Biggest element in list: 1000
Smallest element in list: 21
sum of elements in list: 1691
count the number of elements in list: 6
count the element occurrence no.of times: 2

PREV     NEXT


Share this website !!!
Facebook Twitter Linkedin