Friday, 20 March 2026

Deep in Python list

 

Most Important in Python(0-Z)

1.What is List. Explain in Detail

         The list class is a fundamental built-in data type in Python        

Ø  Ordered: They contain elements or items that are sequentially arranged according to their specific insertion order.

Ø  Zero-based: They allow you to access their elements by indices that start from zero.

Ø  Mutable: They support in-place mutations or changes to their contained elements.

Ø  Heterogeneous: They can store objects of different types.

Ø  Growable and dynamic: They can grow or shrink dynamically, which means that they support the addition, insertion, and removal of elements.

Ø  Nestable: They can contain other lists, so you can have lists of lists.

Ø  Iterable: They support iteration, so you can traverse them using a loop or comprehension while you perform operations on each of their elements.

Ø  Sliceable: They support slicing operations, meaning that you can extract a series of elements from them.

Ø  Combinable: They support concatenation operations, so you can combine two or more lists using the concatenation operators.

In Python, lists are ordered, which means that they keep their elements in the order of insertion

 

Ex: a = [1, 2, 3, 4]

         shopping = ["Milk", "Eggs", "Bread"]

         list_name = [item1, item2, item3]

List is Mutable (VERY IMPORTANT)

👉 You can modify list after creation

Ex:-

Indexing (in List Access Elements):-

You can access an individual object in a list by its position or index in the sequence. Indices start from zero:

Synatx :list_object[index]

           Ex:list[0] – acess First Element in List

 

Type in Command Each below:-

course=["P","Y","T","H","O","N"]
print(course)
#Access First element in List
print("Access First element in List",course[0])
#Access last element in List
print("Access last element in List",course[-1])
#Above statement i don't know string elements that y use this one
print("Access last element in List",course[5])
#Above statement i  know string elements that y use this one
print("Access last using len function element in List",course[len(course)-1])
#aceess First two elements from list
print("Access last element in List",course[0,1]) --error Beasuce Index not work

Slicling:-

Retrieving Multiple Items From a List: Slicing

Syntax:- list_object[start:stop:step]

The [start:stop:step] part of this construct is known as the slicing operator. Its syntax consists of a pair of square brackets and three optional indices, start, stop, and step. The second colon is optional. You typically use it only in those cases where you need a step value different from 1.

  • start specifies the index at which you want to start the slicing. The resulting slice includes the item at this index.
  • stop specifies the index at which you want the slicing to stop extracting items. The resulting slice doesn’t include the item at this index.
  • step provides an integer value representing how many items the slicing will skip on each step. The resulting slice won’t include the skipped items.

                                  Index                           Default Value

                                  start                                    0

                                  stop                         len(list_object)

                                  step                                       1

Example:

letters = ["A", "a", "B", "b", "C", "c", "D", "d"]

I want Upper case letter From List

Index start with hear à0

Stop hear d(Means 8)

Step 2 counter

upper_letters = letters[0::2] –smart way

letters = ["A", "a", "B", "b", "C", "c", "D", "d"]
uppercase=letters[0:8:2]  --Understanding Way this also give same result
print(uppercase)

 

Home work Lower Case you Can try :

 

Pro Level:-

digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#0 to 5 from list
print(digits[0:4]) # Res [0, 1, 2, 3]
print(digits[:4])  # Res [0, 1, 2, 3]
print(digits[-4:]) #res [6, 7, 8, 9]
print(digits[0:len(digits):2]) #res [0, 2, 4, 6, 8]
course=["P","Y","T","H","O","N"]
print(course[::-1]) # res ['N', 'O', 'H', 'T', 'Y', 'P']--revse String

To change the value of a given element in a list, you can use the following syntax:

list_object[index] = new_value

 

In Python, you’ll have two kinds of mechanisms to create copies of an existing list. You can create either:

1.   shallow copy

2.   deep copy

Both types of copies have specific characteristics that will directly impact their behaviour. In the following sections, you’ll learn how to create shallow and deep copies of existing lists in Python. First, you’ll take a glance at aliases, a related concept that can cause some confusion and lead to issues and bugs.

Methods in list:

List Methods

Let's look at different list methods in Python:

  • append(): Adds an element to the end of the list.

Syntax: list_name.append(element)

  • copy(): Returns a shallow copy of the list.

         Syntax: list_name.copy()

  • clear(): Removes all elements from the list.

Syntax: list_name.clear()

  • count(): Returns the number of times a specified element appears in the list.

         Syntax: list_name.count(element)

  • extend(): Adds elements from another list to the end of the current list.

         Syntax: list_name.extend(iterable)

  • index(): Returns the index of the first occurrence of a specified element.

Syntax: list_name.index(element)

  • insert(): Inserts an element at a specified position.

         Syntax: list_name.insert(index, element)

  • pop(): Removes and returns the element at the specified position (or the last element if no index is specified).

Syntax: list_name.pop(index)

  • remove(): Removes the first occurrence of a specified element.

Syntax: list_name.remove(element)

  • reverse(): Reverses the order of the elements in the list.

         Syntax: list_name.reverse()

  • sort(): Sorts the list in ascending order (by default).

Syntax: list_name.sort(key=None, reverse=False)

 

 

Max , Min,and there or more find your self map filter alos those this  your home work

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment

Deep in Python list

  Most Important in Python(0-Z) 1.What is List. Explain in Detail          The  list  class is a fundamental  built-in data type  in Pyt...