![]() |
How To Use Python List With Examples |
How To Use Python List With Examples
Python List: A Python list is a different data types collection.
lost can be used stored list values. The list is created put the comma separately values between square brackets.
Python offers a variety of compound knowledge varieties usually mentioned as sequences.
The list is one among the foremost often used and extremely versatile knowledge varieties employed in Python.
Some Others Programming Language Switch Statement
How To Use Mixed List in Python?
int_list=[11,12,13,14,15 16,17,18,19]
float_list=[4.6,2.6,3.7,8.4,6.3]
Str_list=["java","Python","icoderweb","Google"]
Mix_list=[1234,23.54,"list program"]
print(int_list)
print(float_list)
print(Str_list)
print(Mix_list)
*****OUTPUT*****
[11,12,13,14,15,16,17,18,19]
[4.6, 2.6, 3.7, 8.4, 6.3]
['java', 'Python', 'icoderweb', 'Google']
[1234, 23.54, 'list program']
int_list=[11,12,13,14,15 16,17,18,19]
float_list=[4.6,2.6,3.7,8.4,6.3]
Str_list=["java","Python","icoderweb","Google"]
Mix_list=[1234,23.54,"list program"]
print(int_list)
print(float_list)
print(Str_list)
print(Mix_list)
*****OUTPUT*****
[11,12,13,14,15,16,17,18,19]
[4.6, 2.6, 3.7, 8.4, 6.3]
['java', 'Python', 'icoderweb', 'Google']
[1234, 23.54, 'list program']
Python List Index: Python can be used the index square brackets[] to access an item in a list. In Python, indexes start at 0. So python a list having 10 elements will have an index from 0 to 9.
Trying to access indexes aside from these can raise Associate in Nursing IndexError.
The index should be Associate in Nursing number. we won't use float or alternative sorts, this may end in TypeError.
Nested lists are accessed using nested indexing.
How To Use index List in Python?
data_list=["Python","Java","C"]
print("I Like Language:",data_list[0])
print("I Like Language:",data_list[1])
print("I Like Language:",data_list[2])
*****OUTPUT*****
I Like Language: Python
I Like Language: Java
I Like Language: C
data_list=["Python","Java","C"]
print("I Like Language:",data_list[0])
print("I Like Language:",data_list[1])
print("I Like Language:",data_list[2])
*****OUTPUT*****
I Like Language: Python
I Like Language: Java
I Like Language: C
Python Negative indexing List: Python list allows negative indexing for sequences.
An index of -1 refers to the last item, -2 to the second last item in the python negative list.
How To Use Nagative List in Python?
nag_list=[1,2,3,4,5,6,7,8,9]
print("Printing Negative List:",nag_list[-1])
print("Printing Negative List:",nag_list[-2])
print("Printing Negative List:",nag_list[-3])
print("Printing Negative List:",nag_list[-4])
print("Printing Negative List:",nag_list[-5])
print("Printing Negative List:",nag_list[-6])
print("Printing Negative List:",nag_list[-7])
print("Printing Negative List:",nag_list[-8])
print("Printing Negative List:",nag_list[-9])
*****OUTPUT*****
Printing Negative List: 9
Printing Negative List: 8
Printing Negative List: 7
Printing Negative List: 6
Printing Negative List: 5
Printing Negative List: 4
Printing Negative List: 3
Printing Negative List: 2
Printing Negative List: 1
nag_list=[1,2,3,4,5,6,7,8,9]
print("Printing Negative List:",nag_list[-1])
print("Printing Negative List:",nag_list[-2])
print("Printing Negative List:",nag_list[-3])
print("Printing Negative List:",nag_list[-4])
print("Printing Negative List:",nag_list[-5])
print("Printing Negative List:",nag_list[-6])
print("Printing Negative List:",nag_list[-7])
print("Printing Negative List:",nag_list[-8])
print("Printing Negative List:",nag_list[-9])
*****OUTPUT*****
Printing Negative List: 9
Printing Negative List: 8
Printing Negative List: 7
Printing Negative List: 6
Printing Negative List: 5
Printing Negative List: 4
Printing Negative List: 3
Printing Negative List: 2
Printing Negative List: 1
Python List Slicing: A Python list can be used to access a range of items in a list by using the slicing operator (:).
Slicing can be using access to any elements from the Slicing method.it is the very most important feature of the Python Language.
How To Use Slicing Methods List in Python?
Slice_list1=['I','C','O','D','E','R','W','E','B']
Slice_list2=['P','Y','T','H','O','N']
print("Slicing List First:",Slice_list1[1:4])
print("Slice List Second:",Slice_list2[2:4])
print("Full Slicing List1 Print:",Slice_list1[0:9])
print("Full Slicing List2 Print:",Slice_list2[0:6
*****OUTPUT*****
Slicing List First: ['C', 'O', 'D']
Slice List Second: ['T', 'H']
Full Slicing List1 Print: ['I', 'C', 'O', 'D', 'E', 'R', 'W', 'E', 'B']
Full Slicing List2 Print: ['P', 'Y', 'T', 'H', 'O', 'N']
Slice_list1=['I','C','O','D','E','R','W','E','B']
Slice_list2=['P','Y','T','H','O','N']
print("Slicing List First:",Slice_list1[1:4])
print("Slice List Second:",Slice_list2[2:4])
print("Full Slicing List1 Print:",Slice_list1[0:9])
print("Full Slicing List2 Print:",Slice_list2[0:6
*****OUTPUT*****
Slicing List First: ['C', 'O', 'D']
Slice List Second: ['T', 'H']
Full Slicing List1 Print: ['I', 'C', 'O', 'D', 'E', 'R', 'W', 'E', 'B']
Full Slicing List2 Print: ['P', 'Y', 'T', 'H', 'O', 'N']
Python Elements or Add Elements: A-List is a mutable means elements can be changed in a list.
it can use the assignment operator (=) to change an item in python.
How To Use Change Elements List in Python?
data=[10,20,30,40,50,60,70,80,90]
print("Before Change List:",data)
data[1]=100
print("After Change List:",data)
*****OUTPUT*****
Before Change List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
After Change List: [10, 100, 30, 40, 50, 60, 70, 80, 90]
data=[10,20,30,40,50,60,70,80,90]
print("Before Change List:",data)
data[1]=100
print("After Change List:",data)
*****OUTPUT*****
Before Change List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
After Change List: [10, 100, 30, 40, 50, 60, 70, 80, 90]
Delete Elements list: A Python delete list using the del keyword in python.
it can be used to delete elements and delete list data from the used del keyword.it is the most useful keyword.
How To Delete Elements List in Python?
data_list = ['i','c','o','d','e','r','w','e','b']
del data_list[0:2]
print("Delete Elements:",data_list)
del data_list[3:5]
print("Delete Elements:",data_list)
del data_list
print("Deleted List:",data_list)
*****OUTPUT*****
Delete Elements: ['o', 'd', 'e', 'r', 'w', 'e', 'b']
Delete Elements: ['o', 'd', 'e', 'e', 'b']
NameError: name 'data_list' is not defined
data_list = ['i','c','o','d','e','r','w','e','b']
del data_list[0:2]
print("Delete Elements:",data_list)
del data_list[3:5]
print("Delete Elements:",data_list)
del data_list
print("Deleted List:",data_list)
*****OUTPUT*****
Delete Elements: ['o', 'd', 'e', 'r', 'w', 'e', 'b']
Delete Elements: ['o', 'd', 'e', 'e', 'b']
NameError: name 'data_list' is not defined
Important Some Others Programming Language String
How To Use Python All List Methods?
A Python list method is a very most useful method.it can be every listed program using list methods. list method many types in python Language.
List methods Given Below Details.
append(): Add the part to the top of the list.
extend(): Add all parts of a listing to another list.
insert(): Insert an item at the defined index.
remove(): Removes an item from the list.
pop(): Removes and returns an element at the given index.
clear(): Removes all items from the list.
index(): Returns the index of the primary matched item.
count(): Returns the count of the number of items passed as an argument.
sort(): Sort items in a list in ascending order.
reverse(): Reverse the order of things within the list.
copy(): Returns a shallow copy of the list.
append(): Add the part to the top of the list.
extend(): Add all parts of a listing to another list.
insert(): Insert an item at the defined index.
remove(): Removes an item from the list.
pop(): Removes and returns an element at the given index.
clear(): Removes all items from the list.
index(): Returns the index of the primary matched item.
count(): Returns the count of the number of items passed as an argument.
sort(): Sort items in a list in ascending order.
reverse(): Reverse the order of things within the list.
copy(): Returns a shallow copy of the list.
How To Use All List Methods in Python?
data=[1,2,3,4,5,6,7,8,9,10,5,8,5]
print("Before Printing Data:",data)
data.append(100)
print("Using Append Method:", data)
data.extend([200,300])
print("Using Extend Method:",data)
data.insert(300,400)
print("Using Insert Method:",data)
data.remove(400)
print("Using Remove Method:",data)
data.pop()
print("Using Pop Method:",data)
print("Using Index Method:",data.index(5))
print("Using Count Method:",data.count(5))
print("Before Sort Data:",data)
data.sort()
print("After Sort Data:",data)
datac=data.copy()
print("Using Copy Method:",datac)
data.reverse()
print("Using Reverse Method:",data)
data.clear()
print("List is Cleared:",data)
*****OUTPUT*****
Before Printing Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5]
Using Append Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100]
Using Extend Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300]
Using Insert Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300, 400]
Using Remove Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300]
Using Pop Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200]
Using Index Method: 4
Using Count Method: 3
Before Sort Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200]
After Sort Data: [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 100, 200]
Using Copy Method: [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 100, 200]
Using Reverse Method: [200, 100, 10, 9, 8, 8, 7, 6, 5, 5, 5, 4, 3, 2, 1]
The list is Cleared: []
data=[1,2,3,4,5,6,7,8,9,10,5,8,5]
print("Before Printing Data:",data)
data.append(100)
print("Using Append Method:", data)
data.extend([200,300])
print("Using Extend Method:",data)
data.insert(300,400)
print("Using Insert Method:",data)
data.remove(400)
print("Using Remove Method:",data)
data.pop()
print("Using Pop Method:",data)
print("Using Index Method:",data.index(5))
print("Using Count Method:",data.count(5))
print("Before Sort Data:",data)
data.sort()
print("After Sort Data:",data)
datac=data.copy()
print("Using Copy Method:",datac)
data.reverse()
print("Using Reverse Method:",data)
data.clear()
print("List is Cleared:",data)
*****OUTPUT*****
Before Printing Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5]
Using Append Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100]
Using Extend Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300]
Using Insert Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300, 400]
Using Remove Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200, 300]
Using Pop Method: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200]
Using Index Method: 4
Using Count Method: 3
Before Sort Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 8, 5, 100, 200]
After Sort Data: [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 100, 200]
Using Copy Method: [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 100, 200]
Using Reverse Method: [200, 100, 10, 9, 8, 8, 7, 6, 5, 5, 5, 4, 3, 2, 1]
The list is Cleared: []