List in python Programming language

List in a Python Programming language

In this tutorial, we will discuss the list in python programming language.

The list is a data type in python that is used to store the sequence of data. When creating the list, we can use comma to separate the values between two square bracket- it looks like an array format. Each element of a sequence is named with a variable and assigned a number. It is called an index. Its position in the index is from first to last zero(the first element is 0 and the last element is on)
Syntex of list
list=[element 1,element 2, element 3, element 4, element 5,……., element n];
List index
For an example of the list
Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
Numbers=[“45, “67”, “89”, “76”, “54”, “43”, “32”, “91”];

alphabets=[“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”];

List may be contain multiple type of data

multilist=[“physics”,”chemistry”,”1998″,”2017″];

1. Accessing value in a list

We can accessing data in list using index indice of list

Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];
Numbers=[“45”, “67”, “89”, “76”, “54”, “43”, “32”, “91”];

alphabets=[“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”];

multilist=[“physics”,”chemistry”,”1998″,”2017″];

We can seperate every element of list using follwing code

print(Languages[0]); – Seperate 0 index element from list of Language

print(Numbers[2]); – Seperate 3 index element from list of Numbers

print(multilist[3]); – Seperate 4 index element from list of multilist

 

Example

the output of the above program

Languages[0] – php
Numbers[2] – 89
alphabets[6] – g

multilist[3] – 2017

2. Updating list elements


We can update a single or multiple elements of the list using assign operator given below

print(“New value in the list of languages available in index 2:”)

Languages[2]=”CSS”;  // insert to list of Languages

Example

 

Output above program

Value in the list of languages available in index 2:
java
New value in the list of languages available in index 2:
New value in languages available in index 2:
CSS


3. Delete Elements of list

 

To remove the list element, we can use del statement or remove() method

multilist=[“physics”,”chemistry”,”1998″,”2017″];

print(multilist)

del(multilist[2]) // using del statement

print(multilist)  //print multilist after del index [2] element

multilist.remove(multilist[2]) // remove index[2] element from new list

Example

 

Output of above program

[‘physics’, ‘chemistry’, ‘1998’, ‘2017’]
[‘physics’, ‘chemistry’, ‘2017’]
[‘physics’, ‘chemistry’]

 


4.Insert Elements of the list


We can insert elements of list using insert() method

Numbers=[“45”, “67”, “89”, “76”, “54”, “43”, “32”, “91”];   // list

print(number)

Number.insert(3,100)    // use insert method

print(number)

Example

Output above code

[’45’, ’67’, ’89’, ’76’, ’54’, ’43’, ’32’, ’91’]
[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100]

 

5.Count Elements of the list


We can count element of the list using count() method

Number=[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100];

print(Number)

Number.count(‘100’)    // use count method

print(Number)

Output of above program

[’45’, ’67’, ’89’, ‘100’, ’76’, ’54’, ’43’, ’32’, ’91’, ‘100’]

2


6.Reverse Elements of the list


We can change reverse order element of the list using reverse() method

Number=[’45’, ’67’, ’89’, 100, ’76’, ’54’, ’43’, ’32’, ’91’100];

print(Number)

Number.reverse()

print(Number)

Output above program

[’45’, ’67’, ’89’, ‘100’, ’76’, ’54’, ’43’, ’32’, ’91’, ‘100’]
[‘100′, ’91’, ’32’, ’43’, ’54’, ’76’, ‘100’, ’89’, ’67’, ’45’]

 

7.Append Elements of list

 

Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];

print(Number)

Number.Append([“css”,”Js”,”ruby”])

print(Number)


Output above program

[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]

[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’, [‘css’, ‘Js’, ‘ruby’]]


8.pop Elements of list

 

Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];

print(Languages)

print(Languages.pop(0))

print(Languages)

Example
Output

[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]
php
[‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]



9.sort Elements of list


We can sort object of a list

Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];

print(Languages)

Languages.sort();

print(Languages)

Example




Output

[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]

[‘Asp.net’, ‘C’, ‘C++’, ‘html’, ‘java’, ‘jquary’, ‘php’, ‘vb.net’]

 

10.index Elements of list


Languages=[“php”,”html”,”java”,”C”, “C++”,”vb.net”,”Asp.net”,”jquary”];

print(Languages)

print(Languages.index(“java”));

Example








Output

[‘php’, ‘html’, ‘java’, ‘C’, ‘C++’, ‘vb.net’, ‘Asp.net’, ‘jquary’]

2

Related post

Data type in Python                                        

Tuple in Python                                            Dictionary in Python

Set Data type in Python

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.