Nested List in Python

A-List inside another List is called a Nested List.

Example:

List=[4,5,6,[8,9]]

Four elements are present in the above list, [8,9] is considered as a single element.

To access the elements of the list as

List=[4,5,6,[8,9]]
print(List[0])
print(List[1])
print(List[2])
print(List[3])
##The output is:
4
5
6
[8, 9]

accessing-the-nested-list-element-python

We can access the Nested list elements as

List=[4,5,6,[8,9]]
print(List[3][0])
print(List[3][1])
##The Output is:
8
9

accessing-the-nested-list-element-python-string

Representing the Nested List as Matrix

Python supports the developers to create a matrix by using the nested list.

The following example demonstrates creating a matrix by using a nested list

List=[[10,20,30],[40,50,60],[70,80,90]]
print(List)
print("Elements row wise:")
for x in List:
    print(x)
print("Elements in Matrix style:")
for x in List :
    for y in x:
        print(y,end=' ')
    print()     
##The output is:
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Elements row wise:
[10, 20, 30]
[40, 50, 60]
[70, 80, 90]
Elements in Matrix style:
10 20 30 
40 50 60 
70 80 90 

printing-elements-in-matrix-style

List comprehension in python

The easy and concise way of creating list objects from any iterable objects based on some conditions.

The syntax for the list comprehension is:

List=[expression for an item in list if the condition]

In the normal way we create a list object as follow:

List=[]
for integer in range(0,11):
List.append(integer)
print(List)
##The output is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The same List we can create by using the comprehensive way as follow:

The following program demonstrates the creation of a list element with square values from 1 to 10

List=[x*x for x in range(1,11)]
print(List)
##The output is:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

The next program demonstrates the creation of list elements by considering the values from 1 to 5 to the power of 2

List=[2**x for x in range(1,6)] ## the ** is an exponential operator
print(List)
##The output is:
[2, 4, 8, 16, 32]

The following example demonstrates the creation of a list with elements which are divisible by 10

List=[x for x in range(1,101) if x%10==0]
print(List)
##The output is:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

The next example demonstrates the creation of a list with elements present in the List1 but not in List2

List1=[10,20,30,40]
List2=[30,40,50,60]
List3=[x for x in List1 if x not in List2]
print(List3)
##The output is:
[10, 20]

The next example demonstrates the creation of a list with elements present in both List1 and List2(That means python is going to create a list with the common elements present in both the lists)

List1=[10,20,30,40]
List2=[30,40,50,60]
List3=[x for x in List1 if x in List2]
print(List3)
##The output is:
[30, 40]

The following example demonstrates the creation of a new list with the first letter of every element present in the given list.

List=['Apple','Banana','Carrot','Dragon fruit']
new_List=[word[0] for word in List]
print(new_List)
##The output is:
['A', 'B', 'C', 'D']

The following example demonstrates the creation of a new list from the given list, where every word should be in upper case and representing the number of letters present in each word in a separate list.

string="the quick brown fox jumps over the lazy dog"
words=string.split()
List=[[word.upper(),len(word)] for word in words]
print(List)
##The output is:
[['THE', 3], ['QUICK', 5], ['BROWN', 5], ['FOX', 3], ['JUMPS', 5], ['OVER', 4], ['THE', 3], ['LAZY', 4], ['DOG', 3]]

list-comprehensive-program

The following program demonstrates the display of unique vowels present in the given word

vowels=['a','e','i','o','u']
word=input("Enter any word to search for vowels:")
result=[]
for character in word:
    if character in vowels:
        if character not in result:
            result.append(character)
print(result)
print("The number of unique vowels:",len(result))           
##The output is:
Enter any word to search for vowels:orange
['o', 'a', 'e']
The number of unique vowels: 3

finding-the-vowels-in-the-word

We can shorten the above code as

vowels=['a','e','i','o','u']
word=input("Enter any word to search for vowels:")
result=[ch for ch in vowels if ch in word]
print(result)
print("The number of unique vowels:",len(result))           
##The output is:
Enter any word to search for vowels:swiss
['i']
The number of unique vowels: 1

finding-vowels-in-string

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions