Showing posts with label List manipulation. Show all posts
Showing posts with label List manipulation. Show all posts

List operations

 # List operations 

# Components of a car.


Car_Main_components = ['Chassis', 'Engine', {'Body':['Steering system','Braking system','Suspension']}, {'Transmission_System':['Clutch','Gearbox','Differential','Axle']}]


Car_Auxiliaries = ['car lightening system','wiper and washer system','power door locks system','car instrument panel','electric windows system','car park system']


def fnPrint(L):

for ele in L:

if(isinstance(ele,dict)):

for k, v in (ele.items()):

print("\t",k, ":", ", ".join(v))

continue

print("\t",ele)



print("Car Main Components:",)

fnPrint(Car_Main_components)


print("Car Auxiliaries:")

fnPrint(Car_Auxiliaries)


"""

Sample output

>python ListManip.py

Car Main Components:

         Chassis

         Engine

         Body : Steering system, Braking system, Suspension

         Transmission_System : Clutch, Gearbox, Differential, Axle

Car Auxiliaries:

         car lightening system

         wiper and washer system

         power door locks system

         car instrument panel

         electric windows system

         car park system

"""



List manipulation


# List manipulation

# Creating list

Nterms = int(input("Enter number of terms in List:"))
L = []

print("Enter", Nterms, "numbers : ")
for idx in range(0,Nterms, 1):
ele = int(input())
L.append(ele)
print("Original list : ", L)

# Appending elements to end of list
ele = int(input("Enter element : "))
L.append(ele)

print("Appended list : ", L)

L.reverse()
print("Reversed list : ", L)

ntimes = int(input("Enter number of times to display list elements : "))
print(L*ntimes)

L1 = [1,3,5]
L2 = [2,4,6]

NewList = L1+L2
print("list 1 : ", L1)
print("List 2 : ", L2)
print("Concatenated list : ", NewList)

print(type(L))
SortedList = sorted(NewList)
print("Sorted List : ", SortedList)