list in python with examples- Python List -

list in python with examples- Python List

Hello Guys! If you are searching python list topic in details than you are on right place. In this blog you will learn list in python with examples.

Python is regarded as the language capable of handling large volume of data volume of data termed as ‘Big Data’. To handle such a large amount of data Python introduces a special category data type called ‘sequence’. This category comprises of three data types viz. List, Tuple and Dictionary. 

Python provides several functions and methods to access and process the elements of List. By the end of this chapter you will understand the processes like-traversal of list, joining and replicating the List and Slicing the list etc.


The LIST is one of the sequence data type supported by Python. It can be defined as a collection of values of any data type. A List is always depicted in square brackets. The following are some example of LIST in python.

[] #Empty List
[1,2,3,4,5] #List of Integers
[1.2,2.3,3.4] #List of floating point
[‘x’, ‘y’, ‘z’] #List of Characters
[“Delhi”, “Mumbai”, “Chennai”, “Kolkata”] #List of Strings
[1, “Rajesh”, 12.5,2 “Kiran”, 45.7] #List of Mixed values

One of the most important features of Python List is that it is one of the mutable sequence data type i.e once a List is created and values are stored in it, we can change any of its element in place.

Creating a List:

In Python, the sequence data type List can be created in three ways

1. By Assigning Values
2. Through other sequence
3. Dynamically via Keyboard

1. Creating List by assigning values:

To create a List by assignment method, put a number of data values in square bracket and assign it to a variable.
The data values in square bracket must be separated by commas and the variable to which it is assigned becomes a Python List.

Syntax : <list name>= [<comma separated list of values>]
Example : num=[23,65,42,82,27,19]

print() statement can be used to print the whole List of values, as

Syntax : print(<list name>)
Example : print(num)

 Write a program to create a list of given marks of 10 students in a subject. Also print the List Marks are-

marks= [41,39,44,21,32,47,37,28,48,30]

2. Creating List through other sequences:

We can also create List using any other sequence data type such as string,tuple etc. with the help
of built in Python function list(). The general format is a follows

Syntax : <List name>= list(<any sequence>)

Example : grade= list(“ABCF”)

In the above example we have created a list ‘grade’ using a string “ABCF”. The list() function splits the string into characters and uses
each character as element to creates a List. Following is another example of creating a python List using Python tuple

t= (21.5,23.0,23.8,24.2,25.7)



In the ablove example , we have created a tuple ‘t’ with temperature of a city in past 5 days. Next, we used list() function to create a list ‘temp’
with the values of tuple. Finally print() statement is used to print the List.

Write a program to create a List using a tuple containing price of an item in 5 cities. Also print the list on screen.



3. Creating List dynamically via keyboard:

Using input() statement within eval() function we can create a List dynamically. The input()
statement will take input from the user as a string and eval() function will separate values as element of Python List. Finally list()
function will convert will these values in a List. In this case user has to input values separating with comma.

Syntax : <variable> = eval(input(“<any message>”))
<list name> = list(<variable>)

Example : n=eval(input(“Enter three number :”))


Q) Write a program to input 5 subject marks and create a list using them.

m=eval(input(“Enter 5 subject marks: “))

Accessing the List:

Python List is a mutable sequence data type in which each element can be edited in its place in the List.For editing element we need to access individual element; and Python allow us to access list in both the ways: Whole List at once and Individual element one by one.

Accessing whole List: Python List is a collection of values of several data types stored with a single name.
All the elements of List can do displayed on screen by using List name with print() statement.

Syntax-1 : print(<list name>)

Example : marks= (43,34,27,31,22)

Output : [43,34,27,31,22]

Explanation : in the above example we have created a list of five subject marks with the name ‘marks’. When we print the list name,
all the element gets printed on screen.

Syntax-2 : print(*<list name>)

Example : marks= [43,34,27,31,22]


Output : 43 34 27 31 22

Explanation : in the above example we have created a list of five subject marks with the name ‘marks’. When we print the list name with asterisk sign (*), only the elements get printed on screen without square bracket and comma.

Q) Write a program to create a list of roll no, name and marks obtained of three students and print all the elements on screen.

student=[1,’Naman’, 42,2, ‘Saksham’,45,3, ‘juhi’, 43]

Accessing individual element: Once a List is created and elements are stored in it, the position of each element in the List is
assigned a number. The position of element in the List is called Index and index numbering begins with 0. The diagram below
shows the index position of elements in a List.


To access elements of a List individually we write List name followed by index of the that element in square bracket in the following

Syntax : <List name>[<index of element>]
Example : marks[0]
the above example will display first element of List ‘marks’. ie 11

Q) Write a program to create a list of numbers and print all the numbers separately. Also calculate and print their total.

print(“Forward printing……”)
for a in range(10):

               print(num[a], end=” “)


For More Details Watch our class on Youtube by click on the below link

Traversing the List:

Accesing and processing each and very element of the List is called Traversing the List.For traversal of the List, rather than accessing elements using index number we for() loop
to access elements from the List.

Syntax : for<variable>in<list name>:

body of loop

Example : for a in marks:


Q) Write a program to create a List of 10 numbers. While traversing the List print all its elements and count how many of them are positive, negative and zero.

print(“List elements….”)
for a in num:
print(a,” “,end=””)
elif a<0:
print(“\n\nNo.of positives=”, p)
print(“No. of negatives=”, n)
print(“No. of zeros=”, z)

For More Classes Subscribe Our Youtube channel

Also Check 

python for loop syntax – Looping Statement

Control statements in Python-Conditional Statement

Leave a Comment