Python List with Examples | List in Python 3

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson

What is a List in Python?

List  is a collection of values within pair of square brackets. These values must be separated by commas. These values can be of any data type.

Lists are mutable i.e. we can modify the elements of a list.

1. Creating a List

To create a list, we can put multiple values within the pair of square brackets.

Syntax:

Listname = [value1,value2,value3,……. valueN ]

Listname refers to name of list defined by the programmer.

[ ] Pair of square brackets specifies that it is a list.

value1, value2,value3, …. valueN are different values stored in the list.

Examples:

[ 1,2,3,] List of integers
[ 1,2.5,3,9.5 ] List of numbers(integers and floating point)
[‘a’,’b’,’c’ ] List of characters
[ ‘a’,1,’b’,3.5,’,‘zero’ ] List of mixed values
[‘First’,’Second’,’Third’ ]   List of strings



2. Creating an empty list

Empty list contains no element in it.   It is equivalent of 0 or ‘ ‘(blank).  So it has truth value as False.

We can create an empty list in two ways:

1. By specifying an empty pair of square brackets

Example:

List1=[ ]

Note:  List1 is an empty list containing no element in it.

2. By using list() function without anything within the pair of parenthesis.

Example:

List2=  list ( )

Note:  List2 is an empty list containing no element in it.

3. Creating a Long List

If list contains many  elements which are not  able to fit on one line, we can split it across several lines as given below:

List1=[0,1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,

          41,43,45,47,49]

Opening square bracket appears in the beginning and closing square brackets appears at the end of list.

>>> list1=[10,20,30,40,50,60,70,80,90,100,

               120,130,140,150]

>>> list1

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 130, 140, 150]



4. Nested list

If one list is enclosed within another list, it is known as nested list.

A list can contain another list as its  element. Such a list is called nested list

Example:

List1=[1,2,[5,6,7],9]

List1 is a nested list with four elements:3,4,[5,6,7]and9 .

Here [5,6,7] is nested list.

>>> list2=[1,2,[5,6,7],9]
>>> list2
[1, 2, [5, 6, 7], 9]

5. Creating  a List from Existing Sequence

We can use list() function to create list from existing sequences as given below:

listname=list(<sequence>)

Here, listname refers to name of list specified by the programmer.

<sequence> refers to existing sequence object like string, tuple, set or other list

Python Creates the individual elements of the list from the individual elements of sequence.

Example 1:

Creating a list from existing string
>>> newlist1=list(‘Lovejot’)
>>> newlist1
[‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’]
Description

List named newlist1 is created from string ‘lovejot’

It converts each individual character of string into individual list elements:

[‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’]

Example 2: 

Creating a list from existing tuple
>>> t1=(1,3,5,7,9)
>>> newlist2=list(t1)
>>> newlist2
[1, 3, 5, 7, 9]
Description

List named newlist2 is created from existing tuple t1 = (1,3,5,7,9)

It converts each individual element of tuple into individual list elements:

[1,3,5,7,9]

Example 3: 

Creating a list from existing set
>>> s1={2,4,6,8,10}
>>> newlist3=list(s1)
>>> newlist3
[2,4, 6,8,10]
Description

List named newlist3 is created from existing set s1 = {2,4,6,8,10}

It converts each individual element of set into individual list elements:

[2,4,6,8,10]



6. Reading a List from keyboard

We can also use list() function to read elements of a list by using keyboard.

Example:

Reading list from keyboard
>>> list3=list(input(‘Enter list =’))
Enter list = [‘I’,’n’,’d’,’i’,’a’]
>>> list3
[‘I’, ‘n’, ‘d’, ‘i’, ‘a’]
Description
List name list3 is entered from keyboard as [‘I’,’n’,’d’,’i’,’a’]

7. Accessing elements of a List

Each element of a list has an index associated with it.

Elements of a List are also indexed in two ways:

  1. FORWARD INDEXING AS 0,1,2,3,…
  2. BACKWARD INDEXING AS -1,-2,-3,…

Example

we have a list named list1=[10,30,40,50,60,99]

Elements of above list are indexed as follows:

Index of first element of List is 0 (Forward Indexing) and -6 (Backward Indexing)

So we an access first element of above list as list1[0] or list1[-6]

Similary we can access last element of above list as list1[5] or list1[-1].

Example

>>> classes=[‘xii’,’xi’,’x’,’ix’]
>>> classes[0]
‘xii’     #First element of list is shown.
>>> classes[2]
‘x’     #Third  element of list is shown.
>>> classes[-1]
‘ix’     #Last element of list is shown.
>>> classes[-3]
‘xi’     #Second element of list is shown.

8. Slicing of List elements

We can view specific values in a list by specifying the range of indexes within the pair of square brackets as: Listname[First_index,Last_index,Gap].
Listname is the name of list whose values you want to view.
First_index refers to the starting index.
Last_index refers to ending index.
Gap refers to the gap in indexes

Example

>>> list1=[10,20,30,40,50,60,70]
>>> list1[0:3]
[10, 20, 30]#List elements at indexes 0,1,2 are shown.
>>> list1[2:6]
[30, 40, 50, 60]#List elements at indexes 2,3,4,5 are shown.
>>> list1[-6:-3]
[20, 30, 40]#List elements at indexes -6,-5,-4 are shown.
>>> list1[0:7:2]
[10, 30, 50, 70]#List elements at indexes 0,2,4,6 are shown.
>>> list1[-7:-2:2]
[10, 30, 50]#List elements at indexes -7,-5,-3 are shown.



 9. Find number of elements in list

Function len() of Python returns number of elements in a list. The syntax of len() is

len(listname)

listname refers to existing list whose length we want to fine.

Example

>>> classes=[‘xii’,’xi’,’x’,’ix’]
>>> len(classes)
4#Length of list classes is returned as 4 because there are four values in the list.

10. Membership operators on List

We can use ‘in’ and ‘not in’ operators with Lists

Example:

>>> list1=[10,20,30,40,50,60,70]
>>> 10 in list1
True        #Output is True because 10 is contained in the list.
>>> 25 in list1
False        #Output is False because 25 is not contained in the list.
>>> 25 not in list1
True        #Output is True because 25 is not contained in the list.
>>> 20 not in list1
False      #Output is False because 20 is contained in the list.

 11. Concatenation Operator on List

We can concatenate two lists together by using concatenation operator (+).

Example:

>>> list1=[11,22,33]
>>> list2=[40,50,60]
>>> list1+list2
[11, 22, 33, 40, 50, 60]     #Elements of list1 and list2 are joined together in the output

 12. Replication Operator on List

We can use replication operator (*)to repeat a list.

Example:

>>> list1=[11,22,33]
>>> list1*2
[11, 22, 33, 11, 22, 33]     #Elements of list1 are repeated twice in the output.

13. Comparison of Lists

We can compare two lists using relational operators of Python

If all elements are two lists are same, then two lists are considered equal.  Lists to be compared must be of same type.

Elements  of list are compared one by one. If any comparison is wrong, False is returned.


Example

>>>> list1=[11,22,33]
>>> list2=[11,22,33]
>>> list3=[12,23,34]
>>> list4=[12,25]
>>> list5=[12,23,33]
>>> list1==list2
True #Elements of list1 and list2 are same, So True is returned. 
>>> list1<list2
False#Elements of list1 and list2 are same, So False is returned.
>>> list1<list3
True#All elements of list1 are less than list2, So True is returned.
>>> list1>list3
False#All elements of list1 are less than list2, So False is returned.
>>> list1<list4
True#Only first two elements of list1 are compared to list4. As first two elements of list1 are less than first two elements of list4, True is returned.
>>> list3<list5
False#First two elements of list3 and list5 are same but third element of list3 is not less than list5. So False is returned.




 

Spread the love
Lesson tags: Accessing elements of a List, Concatenation Operator on List, creating a list, Find number of elements in list, list in python, list slicing in python, membership operators with list, read a list, replication operator on list
Back to: Python Programming Tutorial
Spread the love