Skip to main content

List in Python

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





Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...