In this post, we are going to talk about important arrays in Python. It is Python List. We also hope to talk about other array types like dictionaries, tuples, and sets. Before learning about modifiers let's see what are Python lists and what they can do.
What is A-List in Python...
List are you using to store a number of data inside a single data type. Like, putting various color balls into one basket. These lists enable you to access data anytime, get them, and replace or delete data when you want. In Python, we are using [] these brackets to represent a list value. Also, Python lists
are supporting to,
Add unlimited number of duplicate members,
Ordered according to the order that your add list members,
Changeable anytime when you want.
If you refer to our Python Strings tutorial, I told you that Python string works like a list, and it is 0 indexed. As I say Python list items start with the [0]. However, you need to keep in mind when you want to get data from the end it starts with the [-1].
Now Let's Make a List...
a = ["www.", "softexs", ".com"]
print(a)
You can make a list also in this way,
a = list(("www.", "softexs", ".com"))
print(a)
Now Let's see How to Get Items From a List...
In the above, we learned that Python lists are 0 indexed. We are using the same way to get data from a list.
a = ["www.", "softexs", ".com"]
print(a[0])
This is printing the first item on the list.
a = ["www.", "softexs", ".com"]
print(a[-1])
This is printing the last item on the list. because when you get items end to start, it is minus indexed.
Let's see some other tips in the below real-time code.
Now Let's learn how to Write Items on the List...
Now we know reading and getting items from a list. Let's learn about adding and changing items on a list. There few ways to do that.
1. Equal and replace an existing item
2. Using the Insert method
3. Using the Append method
4. Using the Extend method
1. Change Items Using Equal method
In this way, we are getting a list of items like we have done before, and equal that item's index number to a value you want. It is super easy and you can do it for a range of items.
a = ["www.", "softexs", ".com"]
a[2] = ".net"
print(a)
In this method, you can specify the value you want to the index number of the list Items. This is working for every reading method we learned in the real-time code of getting data. Let's see another example.
a = ["www.", "softexs", ".com"]
a[0:2] = ["https", ".net"]
print(a)
You can replace a range of items using this, but you need to keep in mind that you need to make a list of values you are going to replace.
Note: if you put items above the range you got from the parent list, those extra items will show after replaced values in the parent list.
2. Using Insert Method to Add data...
This method is used to add an item to a specific place in the parent list. because this insert() method supports putting an indexed number you want.
a = ["www.", "softexs", ".com"]
a.insert(2, ".org")
print(a)
This method does not replace the existing values, but it is putting a value in the specific indexed number position.
3. Using Append Method to Add data...
This is the common way to add data to a list. But you can't add data to a specific position. It is just adding data to the end of the list. we are using append() syntax to it.
4. Using Extend Method to Add data...
This extend() method is used to merge another data set with a list. In this way, you can add data from another set like tuples to your parent list.
Note: Your second data list might be an iterator. It means that the data set allows looping through it. You can learn more about it by referring to the Python FOR and WHILE loops post.
Now Let's see How to Delete Items on a List...
Now we know how to read, write data on a list. Now we are going to learn about deleting data from lists as in the last part of this post. Here are the main ways to do that.
1. Using Del
2. Using remove()
1. Using the del keword...
del keyword is using to delete a list completely. You can't use this again after deleting whole the list
2. Using the remove() syntax...
remove() method is using to remove a list item by it's name. this is the common way to remove a list item.
Tags:
Python