Total Blog Views: 57
Blog Status: publish
Created By: neyazansari Created at: 08-17-2021
Tags: shadbox python list
The list is very important and you are going to use it many times. The list that you would call a data structure. It's just a way of organizing and storing data in Python. You know about storing a single piece of data that is done through the simple (=) equal operator.
a = 10
x = "hello"
But that's just storing a single piece of data. Sometimes you want to store a group of data that has some sort of connection with each other. For example, if you want to store names of fruits. It doesn't really make sense to store all of the names of each variable. Because they kind of belongs together. They have a relationship with each other. It would be nice if you had a variable that should be called fruit and then you would be able to store all the names of the fruits together in one variable.
In other cases, you might also want to have order in your data. For example, if you are storing all the people in a virtual queue then you want to be able to keep hold of the order in which they join the queue you don't want to let the last person somehow skip the queue because you don't have a good data structure, right? This is why we need to learn about lists and they look very simple:
variable = [ item1, item2, item3, item4]
Those items can be any data type they can even have mixed data types like you could store strings together with numbers or a set of booleans it doesn't really matter. What does matter is the syntax in Python?
x = [123 , "Hello", 12.05]
Here you can see that there are mixed data types in a list variable x. Now if you want to access the first item from a list, you have to access it by its position. The position of the list item always starts from 0. Here is an example.
fruits = ["cherry", "apple", "pear"]
0 1 2
The question arrives why is the first item at position 0. If you think about that index number that is zero, one or two instead of being the position of actually being an offset or a shift from the start of the list. In this case, Cherry is right at the beginning of the list so it has an offset or a shift of 0 but Apple is shifted from the beginning by one and shifted by the offset of 2 and so on. Then it kind of makes more sense than the first item in the list is at the beginning of the list so it has no offset that's why it's zero and you will find that in many programming languages. There are similar data structures to list and this is how they are you will always starting from 0 and then adding by 1.
The indexing from the beginning of the list is called the positive index. But if you want to access the item from the last then you have to use -1 or -2 and so on that is called negative index.
fruits = ["cherry", "apple", "pear"]
-3 -2 -1
print(fruits[-1])
Pythin List Function :
Python has built-in functions that you can one can use on lists.
list.append()
If you want to add an item at the end of the list, which happens most commonly. For example, if you had a list of people coming into your shop, then every subsequent person usually gets added to the end. If you have a new fruit that should be added, after pear. It's possible because of the append function. It will add a single item to the end of the list.
fruits.append("strawberry")
list.extend() - The function adds all elements of a list to another list.
list.insert() - Insert an item at the defined index.
list.remove() -It Removes an item from the list
list.pop() - It removes and returns an element at the given index
list.clear() - Clear all items from the list
list.index() - Returns the index of the first matched item
list.count() - Returns the count of the number of items that is passed as an argument.
list.sort() - Sort items in a list in an ascending order.
list.reverse() - Reverse the order of items in the list.
list.copy() - Returns a shallow copy of the list.
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link