list (1)
list (1)
sequence of element. Each element or value that is inside in list is called item.just as string as
character between quotes list are defined by having value between square brackets[].
>creating list : in python list can be created by placing the element in square brackets[].the
element in the list are separated by the comma[,] the syntax of simple list :
: list_name =[]
:example : intllist =[1,2,3,4]
>travering list : a list is mutable this means we can a use a for loop to traverse all the element in
the list sequentially.
: a =[10,20,20,40]
For item in a:
Print (a)
> indexing : each element in a list can accessed using a technique called
indexing. The index specifies the element to be accessed in the list and
written in square bracket[]. any other sequence type like string or tupleby
their position. Python use zero based indexing which means that the first
element is at 0 the second element at 1 and so on. The index must be
positive ,zero and negative.
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10 (first element)
print(my_list[2]) # Output: 30 (third element)
> negative indexing : we can also use negative indezing. negative indexing
allow you to access element starting form the end of the list. The last
element is -1 and last second element is -1 and so on.
my_list = [10, 20, 30, 40, 50]
print(my_list[-1]) # Output: 50 (last element)
print(my_list[-3]) # Output: 30 (third from last)
>slicing : the concept of list slicing is similar to string slicing list slice
means a part of list.to access some part of list or sublist we use a method
called this can be done by specifying an index range.slicing is a technique
used in python to extract a portion of a list or any sequence (tuple or
string)
list[start:stop:step]
> negative slicing : negative slicing allow you to slice lists from the end.in
python negative indices count backward form the end of the list.
> list modification using slicing : slicing can also be used to modify a list in
place.you can assign a new sequence to a slice to replace the element
within that range.
List = [1,3,5]
list.append(4)
List
1,3,5,4
2.insert() :the method is used to insert an element at specific position in the list,shifting item to
the right the method takes two arguments one index number and second for element value.
listname.insert()
List = [1,3,5]
list.insert(4,4)
List
1,3,5,4,4
3.extend() : this extend () method is used to add content of list 2 at the end of list 1.the extend
modifies the original list.
List 1= [1,3]
List 2 = [2]
list.extend (list2)
4.count() : the count() method return the number of list the specified element appear in the list
listname.count()
List = [1,2,3,2]
list.count(2)
5.index() : the index() method searches the given element form the start of the list and return its
index.if the value appear more than once it will return the index of the first one.if item is not
present it gives error.
listname_index()
List = [3,5,4,3]
list .index(“hello”)
Not present
6.remove() : the method searches for the given element in the list and remove the first matching
element
list_name.remove(element)
List = [34,76,11]
List.remove[76]
7.clear() : the clear method is used to remove all the item from the list.this method is empty the
entire list.
listname.clear()
List = [1,3,4,3]
list.clear()
list= [1,4,6]
max(list)
List = [1,3,5]
min(list)
5.any() : the any() function return the true if any of the boolean values is the true.
any(list_name)
List1 =[1,2,4,5]
any(list)
6.all() : the all function returns true if all the boolean value in the list is true.
Else return false.
all(list_name)
List = [1,4,3,5]
all(list)
7.sorted() : the sorted function return the sorted list of the specifies iterable object.
Sorted function mainly use for descending and ascending order
>nested list :list comprehension are one of the most of amazing feature in python it is smart and
concise way of creating list by iterating over an iterable object. Nested list comprehension is
nothing but a list comprehension withing another list comprehension which is quiet similar to
loops.
Example :
List 1 = [1,3,5]
List 3 = list 1*3
Print (list3)
Output 1,3,5,6,7
Output : true
list = [1,4,6,8]
Print (1 not in the list)
1.less than < : it checks if the left value is lesser than right value
2.greater than > : it check if the right value is greater than left value
3.less than or equal to <= : this operator returns true value only if value on
left is either less than or equal to right of the operator.
4.greater than or equal to : this operator returns true value only if value on
left is greater than or equal to right of the operator.
5.equal(==) : this operator return if the both side of value is equal
6.not equal(!=) : this operator returns true if the value on either side of the
operator are not equal.
>copying list : the simplest way to make a copy of the list is to assign to
another using an assignment operator(=).
1.copying list using slicing : we can slice our original list and store into
new list
Syntax : newlist = oldlist[:]
2.copying list using list function :we can copy list using list function
Syntax newlist = list(oldlist)
3.copying list using copy() : we can copy list using the built in function
copy(). copy() method copies the list and returned the copied list. It does
not take any parameter and return a list.