List In Python In Hindi | Full Explanation Tutorial

Veröffentlicht am: 01 Dezember 2023
auf dem Kanal: SIT AND CODE
17
1

#python #pythondevelopment #pythontutorial #pythonforbeginners #list #listinpython
Python Lists
*Lists are created using square brackets:
*List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
*If you add new items to a list, the new items will be placed at the end of the list

mylist = ["apple", "banana", "cherry"]

Changeable

The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

len, datatype

*A list can contain different data types:

list1 = ["abc", 34, True, 40, "male"]
-------------------------------------------------------------------------------------------------
Access Items

Note: The first item has index 0.

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

*negative indexing also possible
----------------------------------------------------------------
*Range of index

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
-------------------------------------------------------------------------------------
Python - Change List Items

To change the value of a specific item, refer to the index number:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
--------------------------------------------------------------------------------------------
Change a Range of Item Values

replace item with new one

EXAMPLE

Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
---------------------------------------------------------------------------------------------------------
Insert Items

To insert a new list item, without replacing any of the existing values, we can use the insert() method.

The insert() method inserts an item at the specified index:

Example

Insert "watermelon" as the third item:

thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
--------------------------------------------------------------------------------------
Python - Add List Items

To add an item to the end of the list, use the append() method:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
---------------------------------------
Extend List
To append elements from another list to the current list, use the extend() method.

extend() method

Example
Add the elements of tropical to thislist:

thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)

Add Any Iterable
The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).

Example
Add elements of a tuple to a list:

thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
-------------------------------------------------------------------
Remove Specified Item

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Note: if there are multiple same item it will remove first one
---------------------------------------------------------------------
Remove Specified Index

EXAMPLE- remove first item

thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)

Note:If you do not specify the index, the pop() method removes the last item.

syntax- thislist.pop()
----------------------------------
The del keyword also removes the specified index:

EXAMPLE

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
--------------------------------------------------------------------------------------------------
Delete the entire list:

thislist = ["apple", "banana", "cherry"]
del thislist

------------------------------------------------------------------------------------------------------
Clear the List
The clear() method empties the list.
EXAMPLE

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
------------------------------------------------------------------------------------------------
Sort List Alphanumerically
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
----------------------------------------------------------------------------------------------------------------------
Python - Copy Lists
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
--------------------------------------------------------------------------------------------------------------------
Python - Join Lists

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)
-----------------------------------------------------
Python List index() Method

fruits = ['apple', 'banana', 'cherry']

x = fruits.index("cherry")


Auf dieser Seite können Sie das Online-Video List In Python In Hindi | Full Explanation Tutorial mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer SIT AND CODE 01 Dezember 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 17 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!