List in Python

0
What is List Concept in Python:-

1) Dynamic List in Python:-


2) Detailed Description of LIST Object in Python :-




List is a collection of similar and di-similar type of elements ,we can store multiple values using single variable in List.


List provide contiguous memory allocation to store data using proper sequence similar to array of another programming language.

List is a mutable  means we can change list type variable dynamically.

using append() and del()



Syntax of List:-


varname = [ele1,ele2,ele3,....]


x =[12,23,11,78,56]  #same datatype  index 0,1,2,3,4


x = ["hello","welcome",'h','e',1234] #di-similar datatype

Example of List:

x = [12,23,34,45,11,78]
for i in range(0,len(x)):
    print(x[i],'')
 
for i in x:
    print(i)

How we can elements dynamically in List?


size = int(input("enter number of elements for list"))
x = []
for i in range(0,size):
    a = input("enter element to add in list")
    x.append(a)

for a in x:   #for loop without range()
    print(a)

Advantage of List:-

1)  We can store multiple elements in a single variable that can easily perform a logical operation for multiple values.

2)  List store element in a proper order using index hence we can easily search and sort element.


3)  we can dynamically append, insert, and delete elements in List Object.



WAP to calculate the sum of even number and odd number List elements?


x = [12,23,34,67,89,11]
sum1=0
sum2=0
for i in range(0,len(x)):
    if x[i]%2==0:
     sum1=sum1+x[i]
    else:
     sum2=sum2+x[i] 

print(sum1,sum2) 


WAP to find the max element in LIST?

x = [12,23,34,11,67,89,11]
m = x[0]
for i in range(1,len(x)):
    if m<x[i]:
       m=x[i]

print("max element is ",m)


WAP to find the second max element?

x = [100,12,23,34,11,67,89,11,76,98]
m = x[0]
sm=0
for i in range(1,len(x)):
    if m<x[i]:
       sm=m
       m=x[i]
    elif sm<x[i] :
       sm=x[i]
       
print("max element is ",m,sm)     



WAP to find the third max element in List?


WAP to print prime element in List?
x = [2,34,11,67,5,93]
for i in range(0,len(x)):

    count=0
    for j in range(2,x[i]):  #i=2;i<2;i++
        if x[i]%j==0:
            count=1
            break
         
    if count==0:
       print(x[i])
     


WAP to reverse the List element?

WAP to sort List Element?
x = [11,23,24,8,19,2,7]
 
         
for i in range(0,len(x)):
    for j in range(i+1,len(x)):
        if(x[i]>x[j]):
            temp=x[i]
            x[i]=x[j]
            x[j]=temp
     
         
    print(x[i])

 


WAP to merge two lists one list?
[1,2]
[3,4]

o/p [1,2,3,4]

WAP to split one list into two sub-lists?
[1,2,3,4,5,7,8]

o/p [1,2,3,4]
o/p  [5,7,8]

WAP to display unique elements in List?
[1,2,2,3,5,6,9,5]
o/p [1 3 6 9]
Solution:-

x = [2,3,4,3,4,8,7,3]

for i in range(0,len(x)):
    c=0
    for j in range(0,len(x)):
        if x[i]==x[j] and j!=i :
            c=1
            break
         
    if c==0:
        print(x[i])
 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
WAP to display repeated element of LIST but is should display once?
[1,2,2,3,5,6,9,5]
o/p [2,5]


Solution:-

x = [2,3,4,3,4,8,7,3]
y = []
for i in range(0,len(x)):
    c=0
    for j in range(0,len(x)):
        if x[i]==x[j] and j!=i :
            c=1
            break
         
    if c==1:
        if x[i] not in y:
            y.append(x[i])
     
 
print(y)






WAP to display factorial of the list element?


x = [2,3,4,3,4,8,7,3]

for i in range(0,len(x)):
    f=1
    for j in range(1,x[i]+1):
      f=f*j
    print("factorial is ",f)     
 
 

WAP to count data type in the list separately?
............................................................................................................................................

x = [12,"abc","hello",12.34,'a',1,23,1.2]
c1=0
c2=0
c3=0

for i in range(0,len(x)):
    if type(x[i]).__name__=="int":
        c1=c1+1
    elif type(x[i]).__name__=="float":
        c2=c2+1
    else:
        c3=c3+1
print("total integer",c1,"total float ",c2," total string",c3)       


.....................................................................................................................................................

1)  Predefined function of List:-

    1.1) len()  :-    It is used to display size of list

          x=[1,2,3]
          print(len(x))
 
   1.2) type():-    It is used to return data type of the list object

          x=[1,2,3]
         type(x)

 1.3)  sort():-    It is used to sort the list element in ascending order

       x= [4,5,2,7,3]
      x.sort()
      print(x)

1.4)  reverse():-   It is used to reverse the list element 
       x= [4,5,2,7,3]    
      x.reverse()

1.5)  append():-    It is used to add element dynamically in List from the end position
   
       x= [4,5,2,7,3]
      x.append(1)

       print(x)

1.6)   remove():-   It is used to remove the element from List
      x= [4,5,2,7,3]
      x.remove(4)

      print(x)

1.7)  insert():-  using this we can insert an element into a particular position

       x= [4,5,2,7,3]
      x.insert(1,78)    #insert(position,value)

      print(x)



Another article related with LIST:-

1) List Special Operation in Python




2) is, is not an operator and in, not in operator in LIST in Python





Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)