Python Tutorial | Python List | Add Items in the list | Remove Items in list | Join Items in List

Опубликовано: 28 Октябрь 2024
на канале: GSS
142
2

In this video, You will learn Python Programming Language.

Python List
"# Add Items in the list:"
"# append() method: add a item to the end of list"
Mylist = [10, "India", -50, 30.22, True ]
Mylist.append("Delhi")
Mylist.append(100)
print(Mylist)

"# insert method: add item to the specific index"
Mylist = [10, "India", -50, 30.22, True]
Mylist.insert(1, "USA")
print(Mylist)

"# Remove Items in list:"
"# remove method: specific item will be removed"
"# If duplicate items are present in the list then" \
"remove method will remove first item in the list"
Mylist = [10, "India", -50, 30.22, True, 10, 10]
Mylist.remove(10)
Mylist.remove(True)
print(Mylist)

"# pop method: remove specific index from list"
Mylist = [10, "India", -50, 30.22, True, 10, 10]
x = Mylist.pop(1)
print(Mylist)
y = Mylist.pop(3)
print(Mylist)

"# In pop method, if you do not specify the index then" \
"it remove the last item from the list"
Mylist = [10, "India", -50, 30.22, True]
Mylist.pop()
print(Mylist)

"# clear method: empty the list"
Mylist = [10, "India", -50, 30.22, True]
Mylist.clear()
print(Mylist)

"# Copy Items in List:"
"# copy method: Will copy list from one to another"
Mylist = [10, "India", -50, 30.22, True]
CopyMylist = Mylist.copy()
print(CopyMylist)

"# list method:"
Mylist = [10, "India", -50, 30.22, True]
CopyMylist = list(Mylist)
print(CopyMylist)

"# Join Items in List:"
"# Using + Operator:"
city = ["Lucknow", "Mumbai", "Bangalore"]
state = ["UP", "Maharastra", "Karnataka"]
address = city + state
print(address)

"# append method:"
city = ["Lucknow", "Mumbai", "Bangalore"]
state = ["UP", "Maharastra", "Karnataka"]
for i in state:
city.append(i)
print(city)

"# extend method"
city = ["Lucknow", "Mumbai", "Bangalore"]
state = ["UP", "Maharastra", "Karnataka"]
city.extend(state)
print(city)

"# sort method: arrange values in ascending order"
Pincode = [10,20,30, 90, 60, 40, 80, 30]
Pincode.sort()
print("Asending:", Pincode)
Pincode.sort( reverse=10)
print("Descending:",Pincode)

"# reverse method"
Pincode = [10,20,30, 90, 60, 40, 80, 30]
Pincode.reverse()
print(Pincode)

"# Python Tuples:"
See the next video...