- In Python, an inventory may be mutable, which means the values that it contains are often modified.
- An inventory may be a variable that will have a sequence of values assigned thereto during a list, these values are referred to as elements or members.
- It’s a collection that is ordered, changeable, and allows duplicate members.
Defining an inventory
- We define an inventory this way:
- fruits = [“Mango”, “Apple”, “Orange”, “Banana”, “Grapes”]
- In python, lists are written with square brackets.
- We define an inventory by enclosing everything to the proper of the equal check-in square brackets.
- Each element or member is separated by a comma and an area.
- The primary element during a list always has an index of 0, the second element an index of 1, and so on.
- An inventory element is often assigned any sort of value that we will assign to ordinary variables, for instance, a string or variety.
- We will even mix the various sorts of values within the same list.
- For example collection = [“5”,” Abdulmomin”,” Where is”]
- During this example, collection  features a numerical value of 5, collection  features a value of “Abdulmomin”, and collection  features a value of “Where is”.
All the time remember that:
- The primary element during a list always has an index of 0, not 1. this suggests that if the last element within the list has an index of 9, there are 10 items within the list.
- Equivalent naming rules we learned for ordinary variables apply.
- Only letters, numbers, and underscores are legal. the primary character cannot be varied. No spaces.
- It is a good idea to form list names plural—fruits rather than fruit, for example—since an inventory usually contains multiple things.
- We use the append () method to feature an item at the top of the list.
- If we would like to feature “Strawberry”, within the end of the list fruits it’ll be as fruits = [“Mango”, “Apple”, “Orange”, “Banana”, “Grapes”, “Strawberry”]
- The statement begins with the list name: fruits.append(“Strawberry”)
- Next, there is a dot: fruits.append(“Strawberry”)
- Then the keyword append fruits. append(“Strawberry”)
- Rather than appending a component to the top of an inventory, we will insert it into the list where we would like it.
- We use the insert () method to feature an item at the required index.
Delete and take away elements
- There are several methods to get rid of items from an inventory.
- We will delete any list element by specifying its index.
- The remove () method removes the required item.
- The pop () method removes the required index. we will pop a component of an inventory and append it or insert it to a different list.
- The del keyword removes the required index. It also can delete the list completely.
- Similarly, the clear () method empties the list.
- We will copy consecutive elements of an inventory to create another list.
- The primary number inside the brackets targets the primary element within the slice.
- Use the len () function to work out what percentage items during a list.fruits = [“Mango”, “Apple”, “Orange”, “Banana”, “Grapes”, “Straw berry”]Result: 6
Example: Print the number of things within the list:
Concatenation of the list
- In python, two or more lists are often joined or concatenate.
- One among the simplest ways are by using the + operator.List4 = [“x”, “y” , “z”]List6 = list4 + list5
- List5 = [5, 6, 7]
- Example: Join two list:
- Result: [ “x”.”y”.”z”,5,6,7]
- We will join two lists by appending all the things from list4 into list5, one by one.
- We will also use the extend () method, which purpose is to feature elements from one list to a different list.
- We can use on lists the subsequent set of python built-in Methods.
append() Adds a component at the top of the list
insert() Adds a component at the required position
extend() Add the weather of an inventory to the top of the present list
pop() Removes the element at the required position
remove() Removes the item with the required value
clear() Removes all the weather from the list
count() Returns No. of elements with the required value
copy() Returns a replica of the list
index() Returns index of 1st element with specified value
reverse() Reverses the order of the list