Lists in python
Lists are used to store multiple items. List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1] etc. Lists also have negative index number starting from [-1] from the last element of the list.
Example of list with numbers
li=[10,20,30,40,50]
print(li)
Output
[10, 20, 30, 40, 50]
Example of list with float
li=[10.23,20.12,30.34,40.21,50.32]
print(li)
Output
[10.23, 20.12, 30.34, 40.21, 50.32]
Example of list with string
courses=["python","c","c++","java","oracle"]
print(courses)
Output
['python', 'c', 'c++', 'java', 'oracle']
Example of list containing values of different types
li=[10,20.12,"python"]
print(li)
Output
[10, 20.12, 'python']
Example of list using for loop
li=[10,20,30,40,50]
for i in li:
print(i,end=' ')
Output
10 20 30 40 50
Example of list using positive index number
li=[10,20,30,40,50]
print(li[1])
Output
20
Example of list using negative index number
li=[10,20,30,40,50]
print(li[-1])
Output
50
Example of joining lists
lst1=[10,20,30]
lst2=[40,50,60]
lst3=lst1+lst2
print(lst3)
Output
[10, 20, 30, 40, 50, 60]
List slicing
Example1 of list slicing
li=[10,20,30,40,50]
print(li[1:4])
Output
[20, 30, 40]
Example2 of list slicing
li=[10,20,30,40,50]
print(li[:])
Output
[10, 20, 30, 40, 50]
Example3 of list slicing
li=[10,20,30,40,50]
print(li[2:])
Output
[30, 40, 50]
Example4 of list slicing
li=[10,20,30,40,50]
print(li[:4])
Output
[10, 20, 30, 40]
Example5 of list slicing
li=[10,20,30,40,50]
print(li[1:-2])
Output
[20, 30]
Declare an Empty List in Python
Declaring an empty list in python can be achieved in two ways:
- By using square brackets[]
- By using the list() constructor
Create an Empty List in Python using Square Brackets []
To declare an empty list just assign a variable with square brackets.
Example:
# Code to declare empty list mylist=[] print("Values of mylist:", mylist) print("Type of mylist:", type(mylist)) print("Size of mylist:", len(mylist))
Output
Values of mylist: []
Type of mylist: <class 'list'>
Size of mylist: 0
Type of mylist: <class 'list'>
Size of mylist: 0
Create an Empty List using the list() Constructor
The list() constructor is used to create a list in Python.
# Program to create empty list # list is declared using constructor() mylist=list() print("Values of mylist:",mylist) print("Type of mylist:",type(mylist)) print("Size of mylist:",len(mylist))
Output
Values of mylist: []
Type of mylist: <class 'list'>
Size of mylist: 0
Type of mylist: <class 'list'>
Size of mylist: 0
Python Append to Empty List
In Python, we can create an empty list and then later append items to it, using the Python append() function.
# create an empty list mylist= [] # append items to an empty list mylist.append(100) mylist.append(200) mylist.append(300) mylist.append(400) print("Values of mylist:",mylist) print("Type of mylist:",type(mylist)) print("Size of mylist:",len(mylist))
Output
Values of mylist: [100, 200, 300, 400]
Type of mylist: <class 'list'>
Size of mylist: 3
Type of mylist: <class 'list'>
Size of mylist: 3