Nested List

 # Nested List


"""

Create a nested list with each list containing details of department name, code and class strength. 

1. Print all elements

2. Print department names

3. Print department name and code 

4. Print total student strength

5. Create and Print a dictionary - {"departmentname": [code, strength]}

"""


NL = [["IT",205,56],["Mech",114,55],["CSE",104,58],["EEE",105,60],["ECE",106,52]]


# Print all elements

print("Print all elements")

for ele in NL:

print(ele)


# Print Department names

print("Department : ",end=" ")

for ele in NL:

print(ele[0],end=" ")


# Print Department name and code

print("\nDepartment name and code:")

sum=0

for dept,code,cs in NL:

print(dept,":",code)

sum+=cs


print("Total student strength = ",sum)


# Dictionary


D={}

for dept,code,cs in NL:

    D[dept] = [code,cs]

   

print("Dictionary:",D)


"""

Sample output

>python Nestedlist.py

Print all elements

['IT', 205, 56]

['Mech', 114, 55]

['CSE', 104, 58]

['EEE', 105, 60]

['ECE', 106, 52]


Department :  IT Mech CSE EEE ECE


Department name and code:

IT : 205

Mech : 114

CSE : 104

EEE : 105

ECE : 106


Total student strength =  281


Dictionary: {'IT': [205, 56], 'Mech': [114, 55], 'CSE': [104, 58], 'EEE': [105, 60], 'ECE': [106, 52]}


"""

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu