0% found this document useful (0 votes)
3 views

list (1)

The document provides a comprehensive overview of lists in Python, detailing their mutable nature, creation, traversal, indexing, slicing, and various methods for modification and querying. It explains built-in functions related to lists, operations such as concatenation and membership checking, and techniques for copying lists. Additionally, it covers nested lists and list comprehension, making it a thorough guide for understanding and working with lists in Python.

Uploaded by

purabupadhyay06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

list (1)

The document provides a comprehensive overview of lists in Python, detailing their mutable nature, creation, traversal, indexing, slicing, and various methods for modification and querying. It explains built-in functions related to lists, operations such as concatenation and membership checking, and techniques for copying lists. Additionally, it covers nested lists and list comprehension, making it a thorough guide for understanding and working with lists in Python.

Uploaded by

purabupadhyay06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

> What is list : a list is data structure in a python that is mutable or changeable ordered

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]

1.​ Start : the index to start slicing (inclusive)


2.​ Stop : the index to stop slicing(exclusive)
3.​ Step : the step size or stride,default is 1 which means every element
between start and stop included.

> negative slicing : negative slicing allow you to slice lists from the end.in
python negative indices count backward form the end of the list.

-1 refers to the last element


-2 refers to the last second element. And so on.

lst = [10, 20, 30, 40, 50, 60]


print(lst[-3:]) # Output: [30, 40, 50, 60] - starts from the third last
element to the end
print(lst[:-3]) # Output: [10, 20, 30] - up to the third last element

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

example my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


my_list[2:5] = [10, 11, 12] # Replaces elements at indices 2, 3, and 4
print(my_list) # Output: [0, 1, 10, 11, 12, 5, 6, 7, 8, 9]
>list method 👍
1.append() : the append method is used for appending and adding element to a list . it is used
add element at last position of the list this method does not return new list and it just modifies
the original list.
list_name.append()

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()

> built in fuctoion list :


1.len() : the len function return the length of string.
Syntax : len(list_name)
Example : list 1 = [1,3,5,6]
len(list1)
Output : 4

2.sum () : the method used to calculate the sum of all element.


sum(list_name)
Example list = [1,4,6,7]
sum(list)
Output : 17

3.max() : the max function returns the largest item in an iterable.


max(list_name)

list= [1,4,6]
max(list)

4.min() : the min function returns the smallest item in an iterable.


min(list_name)

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.

Syntax : new_list = [[expression for item in list] for item in list]

>basic list operation


1.​ concatenation : concatenation two lists means joining two lists. We
can concatenate two list using concatenation operator plus which is
denoted by + . the + operator cannot add list with other type as
number or string because this operator is used only with list types.

>syntax : list = list 1 + list 2


Example
List 1 = [1,2,3]
List 2 =[2,4,3]
List 3 = list 1 + list 2
Print (“concatenate list =”,list3)

:output : concatenate list =[1,2,4,5,6]

2.repetition of list(replicating list) : python allows us to repeat the


element of the list using repetition operator which is denoted by symbol
*.we can also assign the repetition value of the list with another list.

Example :
List 1 = [1,3,5]
List 3 = list 1*3
Print (list3)
Output 1,3,5,6,7

3.membership operator (in or not) : the “in” operator is type of


membership operator. It is used to check if a particular element is present
in or not. The output given by “in” operator is same as boolean operator. It
give as output as either true and false. The “in” operator return true if the
operand appears in the sequence in the right sequence otherwise it return
false.
Example : list = [1,4,6,8]
Print (1 in the list)

Output : true

The “not in “ operator is also a type of membership operator. It is used to


check if a particular element is not present in the list. It is also same as
boolean operator . it give output as either true and false.

list = [1,4,6,8]
Print (1 not in the list)

> comparing operator : a comparison operator in python also called


python relational(<,>,==, !=,>=,,=) that compare the two value of two
operands and return true and false value based output

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.

You might also like