التخطي إلى المحتوى الرئيسي

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





تعليقات

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...