A list in Python is used to store the sequence of diverse kinds of data. Python lists are mutable kind it is mean we can modify its element after creation. However, Python includes six information sorts that are capable of shop the sequences, but the maximum commonplace and dependable type is the listing.
A list may be a set of values or objects of different sorts. The gadgets in the list are separated with the comma (,) and enclosed with the rectangular brackets .
Characteristics of Python List
- The lists are in order form.
- The element of the list can get the right of entry using an index.
- The lists are the mutable type.
- The lists are mutable sorts.
- A list can keep the variety of numerous factors.
Accessing Python Lists
There are some keywords we use for accessing values in python list. They are:
This method uses the listing comprehension method for the equal, this reduces the possible traces of code and therefore saves time.
Demonstrate List comprehension
L1 = [10, 40, 50, 60, 70] print("Original list is : " + str(L1)) print("List index-value are : ") print([list((i, L1[i])) for i in range(len(L1))])
Original list is : [10, 40, 50, 60, 70] List index-value are : [[0, 10], [1, 40], [2, 50], [3, 60], [4, 70]] >
Python List Enumerate
This is the maximum fashionable method to perform this particular trouble and is quite endorsed to be used in case we require to get the index together with the cost within the listing.
Example to understand Enumerate
L1 = [10, 40, 50, 60, 70] print([list((i, L1[i])) for i in range(len(L1))]) print("Original list is : " + str(L1)) print("List index-value are : ") for index, value in enumerate(L1): print(index, value)
[[0, 10], [1, 40], [2, 50], [3, 60], [4, 70]] Original list is : [10, 40, 50, 60, 70] List index-value are : 0 10 1 40 2 50 3 60 4 70
List append() method
One can easily update a single or more than one element of lists with the aid of giving the slice at the left-hand side of the challenge operator, and you could upload to elements in a list with the append() approach.
Demonstrate append() method
L1 = [10, 40, 50, 60, 70] L2 = ['physics', 'chemistry', 1997, 2000] print("Value available at index 2: ") print(L2) L2 = 2001 print("New value available at index 2 : ") print(L2)
Value available at index 2: 1997 New value available at index 2 : 2001
List remove() method
There are numerous strategies on the listing information kind that assist you to put off detail from a given listing. The strategies are get remove(), pop() and clear(). Besides the list techniques, you can also use a del keyword to take away items from a list. As all these statements have the same use we will be taking a look at the remove() method.
Demonstrate remove() method
list1 = [12, 'Sonu', 'Tina', 14, 'Monu', 12, 'Riya'] list1.remove(12) print(list1) list1.remove('Riya') print(list1)
['Sonu', 'Tina', 14, 'Monu', 12, 'Riya'] ['Sonu', 'Tina', 14, 'Monu', 12]
List Indexing approach lets you find the primary lowest index of the given element. Slicing in the list is not an unusual exercise and it is the most used approach for programmers to remedy green troubles. Python offers us the ability to symbolize these facts with help of lists in form of matrices. Python permits builders to enforce matrices the usage of the nested list.