Please Subscribe My YouTube Channel

Thursday, March 16, 2023

Python Program : List Method

Python Program : List Method

#list is a collection of diffrent types of data.
#enclosed with square brackets
#list elements seperated by comma
#It may be empty
#for creating a list we use list construtor or method
#list is mutable or changable (we can change / add/ remove / access list items)


# create an empty list
list1= []
print(list1)

#list with mixed data types
#with int string and float, tuple, list and set value.      

list2 = [1, "Hello", 3.4, (1,2,4), [1,3,4], {1,2,3}] 
print(list2)

#creating list with list constructor
tl= list(("apple", "banana", "cat"))
print(tl) #['apple', 'banana', 'cat']

#Acess list items
plang = ["Python", "Swift", "C++","Kotlin","C","C#"]

# access item at index 0
print(plang[0])# result:- Python

# access item at index 2
print(plang[2])   # C++
# access item at index 5
print(plang[5])   # C#

# access item at negative index -1
print(plang[-1])   # C#
# access item at negative index -2
print(plang[-2])   # C#

#access nested list element
list1=[5,3.2,'ram',6,8,9,[8,5,6]]

print(list1[6][1]) #5

#List slicing in Python

my_list = ['I','T','C','I',' ','H','A','R','D','O','I']

#items from index 2 to index 4
print(my_list[0:4]) #result- ['I','T','C','I']

# items from index 5 to end
print(my_list[5:]) #result:- ['H','A','R','D','O','I']
# result items beginning to end
print(my_list)
print(my_list[:])
print(my_list[:11])
print(my_list[-11:])


# Add element using append method in list
numbers= [21, 34, 54, 12]
print("Before Append:", numbers)
# after using append method
numbers.append(32) #it takes exactly one argument in one time
print("After Append:", numbers)

#with extend method you can add a list element in another list (join two lists)
#using extend method here you can also add any iterable object (tuples, sets, dictionaries etc.).
num1 = [2, 3, 5]
print("List1:", num1)
num2 = [4, 6, 8]
print("List2:", num2)
num2.extend(num1)
print("List after extend:", num2)

#or
fr_list = ["apple", "banana", "cherry"]
other_list = list(fr_list)
print(other_list)

#or using + concatenate operator
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

#Creating nested list
list1 = [1, 2, 3, 4, 5]
print(list1)
list2 = [12, 13, 23]
print(list2)
list3 = [10, 20, 30]
print(list3)
NestedList = [list1, list2, list3, list4, list5]
print("The list of lists or Nested list is:")
print(NestedList)

#copy list items in another empty list
g= ["apple", "baby", "ambuj"]
a = g.copy()
print(a)

#or you can copy
g= ["anand", "vartika", "chaya"]
a = list(g)
print(a)



#insert an item in list without removing existing element
thislist = ["apple", "banana", "cow"]
thislist.insert(2, "water") 
print(thislist) # here result will be ["apple", "banana", "water","cow"]

# changing list item:(it's mutable so we can change list items.
color= ['pink', 'red', 'gray']

# changing the third item to 'C'
color[2] = 'green'

print(color)  # ['pink', 'red', 'green']

# changing the list items acc to range(change range)
th = ["apple", "blue", "cool", "orange", "kiwi", "mango"]
print(th) #["apple", "blue", "cool", "orange", "kiwi", "mango"]
th[1:3] = ["black", "bag"]
print(th) #["apple", "black", "bag", "orange", "kiwi", "mango"]

#pop method for using last element from list or remove item acor to index value
t = ["apple", "banana", "cherry"]
t.pop()
print(t) #["apple", "banana"]

t1=["ajay", "vijay", "chetan"]
t1.pop(2)
print(t1)

#remove method for remove any specific element
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']
prog.remove('Java')
print(prog)

#remove list items using del keyword
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']

# deleting the second item
del prog[1]
print(prog) # ['Python', 'C++', 'C', 'Java', 'Ruby', 'php']

# deleting the last item
del prog[-1]
print(prog) # ['Python', 'C++', 'C', 'Java', 'Ruby']

# delete first two items
del prog[0 : 2]  # ['C', 'Java', 'Ruby']
print(prog)


# del all list in one time with del and clear
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']
del prog

prog1= ['Python', 'Swift', 'C++', 'C']
prog1.clear()
print(prog1) #return empty list

#creating a list with for loop
for i in range(6):
         print(i)
#print all items from list by using loop method
z=['a','b','c','d']
for i in (z):
    print(i)
#append item from one list to another list using loop
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
  if "a" in x:
    newlist.append(x)

print(newlist) #the result will be ["apple", "banana", "mango"] bcz a is found in all items

#sort the list items
fruit = ["orange", "mango", "kiwi", "pineapple", "banana"]
fruit.sort()
print(fruit) #['banana', 'kiwi', 'mango', 'orange', 'pineapple']

num= [100, 50, 65, 82, 23]
num.sort()
print(num)

#sort list in descending order
num= [100, 50, 65, 82, 23]
num.sort(reverse = True)
print(num)

#count the items from list

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

x = fruits.count("cherry")

print(x) #result:- 2

#find index value from list items
fruits = ['apple', 'banana', 'cherry']

x = fruits.index("banana")

print(x) #index value is 1






















































No comments:

Post a Comment

*******Please Comment, Like and Share******
दोस्तों पूरी पोस्ट पढने के लिए धन्यवाद, आशा करता हूँ आप को यह पोस्ट पसंद आई होगी और भी अधिक जानकारी के लिए या मुझसे जुड़ने के लिए आप मुझे मेरे फेसबुक पेज information & technology computer institutions और इन्स्टाग्राम ID:- satyam_hardoi को फॉलो कर सकते है,
मेरा youtube चैनल itci computer hardoi सर्च करे और पाए कंप्यूटर से सम्बंधित सभी विडियो और हाँ प्लीज नई अपडेट पाने के लिए चैनल को सब्सक्राइब जरुर करें, धन्यवाद | आपका $ सत्यम सर् $