Python Menu

List Sequence in Python


In python, a list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].


Syntax:
List = [value1, value2, value3,….]

 Python Example Program - List in Python

Example:
L0 = []		#creates empty list
L1 = [123,"python", 3.7]  
L2 = [1, 2, 3, 4, 5, 6]  
L3 = ["C Programing","Java","Python"] 
print(L0)
print(L1)
print(L2)
print(L3)
Output:
[]
[123, 'python', 3.7]
[1, 2, 3, 4, 5, 6].
['C Programing','Java','Python']

Python List indexing

The indexing are processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].

The index starts from 0, the first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.


list indexing


Python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the list has the index -1, its adjacent left element is present at the index -2 and so on until the left most element is encountered.

Python List Operators

Operator Description
+ It is known as concatenation operator used to concatenate two lists
* It is known as repetition operator. It concatenates the multiple copies of the same list.
[ ] It is known as slice operator. It is used to access the list item from list.
[ : ] It is known as range slice operator. It is used to access the range of list items from list.
in It is known as membership operator. It returns if a particular item is present in the specified list.
not in It is also a membership operator and It returns true if a particular list item is not present in the list.

 Python Example Program - List Operators in Python

Example:
num=[1,2,3,4,5]
lang=['python','c','java','php']
print(num+lang)
print(num*2)
print(lang[2])
print(lang[1:4])
print('cpp' in lang)
print(6 not in num)
Output:
[1, 2, 3, 4, 5, 'python', 'c', 'java', 'php']
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
java
['c', 'java', 'php']
False
True

How to add or change elements to a list?

Python allows us to modify the list items by using the slice and assignment operator.

We can use assignment operator (=) to change an item or a range of items.

 Python Example Program - to add or change elements to a python list

Example:
num=[1,2,3,4,5]
print(num)
num[2]=30
print(num)
num[1:3]=[25,36]
print(num)
num[4]="Python"
print(num)
Output:
[1, 2, 3, 4, 5]
[1, 2, 30, 4, 5]
[1, 25, 36, 4, 5]
[1, 25, 36, 4, 'Python']

How to delete or remove elements from a list?

Python allows us to delete one or more items in a list by using the del keyword.

 Python Example Program - to delete or remove elements from a python list

Example:
num=[1,2,3,4,5]
print(num)
del num[1]
print(num)
del num[1:3]
print(num)
Output:
[1, 2, 3, 4, 5]
[1, 3, 4, 5]
[1, 5]

Python List Functions

Python provides the following built-in functions which can be used with the lists.

  • len()
  • max()
  • min()
  • list()
  • sum()
  • sorted()
  • append()
  • remove()
  • sort()
  • reverse()
  • count()
  • index()
  • insert()
  • pop()
  • clear()

☞ len()

In Python len() is used to find the length of list,i.e it returns the number of items in the list.

Syntax:
len(list)

 Python Example Program - len() in python list

Example:
num=[1,2,3,4,5,6]
print(“length of list :”,len(num))
Output:
length of list : 6

☞ max()

In Python max() is used to find maximum value in the list

Syntax:
max(list)

 Python Example Program - max() in python list

Example:
num=[1,2,3,4,5,6]
lang=['java','c','python','cpp']
print("Max of list :",max(num))
print("Max of list :",max(lang))
Output:
Max of list : 6
Max of list : python

☞ min()

In Python min() is used to find minimum value in the list

Syntax:
min(list)

 Python Example Program - min() in python list

Example:
num=[1,2,3,4,5,6]
lang=['java','c','python','cpp']
print("Min of list :",min(num))
print("Min of list :",min(lang))
Output:
Min of list : 1
Min of list : c

☞ sum()

In python, sum(list) function returns sum of all values in the list. List values must in number type.

Syntax:
sum(list)

 Python Example Program - sum() in python list

Example:
num=[1,2,3,4,5,6]
print(“sum of list items :”,sum(num))
Output:
sum of list items: 21

☞ sorted()

In python, sorted(list) function is used to sort all items of list in an ascending order.

Syntax:
sorted(list)

 Python Example Program - sorted() in python list

Example:
num=[1,3,2,4,6,5]
lang=['java','c','python','cpp']
print(sorted(num))
print(sorted(lang))
Output:
[1, 2, 3, 4, 5, 6]
['c', 'cpp', 'java', 'python']

☞ list()

The list() method takes sequence types and converts them to lists. This is used to convert a given string or tuple into list.

Syntax:
list(sequence)

 Python Example Program - list() in python list

Example:
str="python"
list1=list(str)
print(list1)
Output:
['p', 'y', 't', 'h', 'o', 'n']

☞ append()

In python append() method adds an item to the end of the list.

Syntax:
list.append(item)

-item may be number,string,list and etc

 Python Example Program - append() in python list

Example:
num=[1,2,3,4,5]
lang=['python','c','java','php']
num.append(6)
print(num)
lang.append("cpp")
print(lang)
Output:
[1, 2, 3, 4, 5, 6]
['python', 'c', 'java', 'php', 'cpp']

☞ remove()

In python remove() method removes the first item from the list which is equal to the passed value. It throws an error if the item is not present in the list.

Syntax:
list.remove(item)

-item may be number,string,list and etc

 Python Example Program - remove() in python list

Example:
num=[1,2,3,4,5]
lang=['python','c','java','php','c']
num.remove(2)
print(num)
lang.remove("c") # first occurence will remove
print(lang)
lang.remove("cpp")
print(lang)
Output:
[1, 3, 4, 5]
['python', 'java', 'php', 'c']
ValueError:list.remove(x):x not in list

☞ sort()

In python sort() method sorts the list elements. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the list into descending order. By default, list sorts the elements into ascending order.

Syntax:
list.sort ([reverse=true])

-reverse, which displays items in descending

 Python Example Program - sort() in python list

Example:
lang = ['p', 'y', 't', 'h', 'o','n'] # Char list  
even = [6,8,2,4,10] # int list  
print(lang)  
print(even)  
lang.sort()  
even.sort()  
print("\nAfter Sorting:\n")
print(lang)  
print(even) 
print("In Descending Order :\n")
even.sort(reverse=True)
print(even) 
Output:
['p', 'y', 't', 'h', 'o', 'n']
[6, 8, 2, 4, 10]
After Sorting:
['h', 'n', 'o', 'p', 't', 'y']
[2, 4, 6, 8, 10]
In Descending Order :
[10, 8, 6, 4, 2]

☞ reverse()

In python reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index.

Syntax:
list. reverse ()

 Python Example Program - reverse() in python list

Example:
lang = ['p', 'y', 't', 'h', 'o','n']
lang = ['p', 'y', 't', 'h', 'o','n'] 
print("After reverse")
lang.reverse()
print(lang) 
Output:
After reverse
['n', 'o', 'h', 't', 'y', 'p']

☞ count()

In python count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0.

Syntax:
list.count(item)

 Python Example Program - count() in python list

Example:
num=[1,2,3,4,3,2,2,1,3,4,5,7,8]
cnt=num.count(2)	
print("Count of 2 is:",cnt)
cnt=num.count(10)	
print("Count of 10 is:",cnt) 
Output:
Count of 2 is: 3
Count of 10 is : 0

☞ index()

In python index () method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If list contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.

Syntax:
list.index(x[, start[, end]])

 Python Example Program - index() in python list

Example:
lang = ['p', 'y', 't', 'h', 'o', 'n', 'p','r','o','g','r','a','m'] 
print("index of t is:",lang.index('t'))
print("index of p is:",lang.index('p'))
print("index of p is:",lang.index('p',3,10))
print("index of p is:",lang.index('z'))
Output:
index of t is: 2
index of p is: 0
index of p is: 6
ValueError: 'z' is not in list

☞ insert()

In python insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element.

Syntax:
list.insert(i,x)

i : index at which element would be inserted.
x : element to be inserted.

 Python Example Program - insert() in python list

Example:
num=[10,20,30,40,50]
num.insert(4,60)
print("updated list is :",num)
num.insert(7,70)
print("updated list is :",num)
Output:
updated list is : [10, 20, 30, 40, 60, 50]
updated list is : [10, 20, 30, 40, 60, 50, 70]

☞ pop()

In python pop() element removes an element present at specified index from the list. It returns the popped element.

Syntax:
list.pop([i])

 Python Example Program - pop() in python list

Example:
num=[10,20,30,40,50]
num.pop()
print("updated list is :",num)
num.pop(2)
print("updated list is :",num)
num.pop(7)
print("updated list is :",num)
Output:
updated list is : [10, 20, 30, 40]
updated list is : [10, 20, 40]
IndexError: pop index out of range

☞ clear()

In python clear() method removes all the elements from the list. It clears the list completely and returns nothing.

Syntax:
list.clear()

 Python Example Program - clear() in python list

Example:
num=[10,20,30,40,50]
num.clear()
print("After clearing ",num)
Output:
After clearing [ ]

Next Topic :Tuple sequence in Python