This is a text field

How python list is best or (“not so good”) in storing data?

Image credit: GangBoard.com

Python list storing the data. You can store changeable, mutable and duplicate items in a python list.

With the help of the Python list, you can change the group of items.

The list can be shown by the square bracket([]).

The items are stored inside the square brackets([]).

For example,[2,34].

The list of numbers can be shown by:-

Output

There are many powerful methods that you can use in the python list.


Add elements in python list

You can add elements to the python list using the append, extend and insert method.

Add at last– append method

Image credit: freecodecamp

It’s a unique way to add elements to the list compared to the strings or integers.

You can add elements, at last, using variable.append(value). This will make a big list of elements.

For example,

Output:

In the above example, you can see that how the list has become big.

It’s proof that you can make list as big as you want using the append method.


Warning: The append method add an element at last. You can't add an element at the required position using the append method.

Add at the required position- insert() method

There is an alternative method to add an element to the list. But this element adds the required position, not at the last of the list.

You can connect with this example.

For example,

Output:

From the above’s example, you can observe that the insert() method add an element at a required position not at last.


Extend the list: add one list into another

Image credit: afternerd

The extend() method extend the list by adding one list into another list.

It’s the merger of the two lists.

You can relate to this example.

For example,

Output:

In this example, you can see that how strongly one list has been added to another list and created a new one.

If you have a keen eye to see, you can observe that the other list has been added to the last of the first list.


Remove element: make a short and clear list

Image credit: edureka

Removing elements is a need in the python list.

You can remove elements in the following method:

• remove() method
• pop() method
• del keyword
• clear() method

The remove() method

You can use the remove() method to remove the elements from the list.

For example,

Output:

In the above example, you can see that the list has become short and clear.

With the help of the remove() method, you can remove the unnecessary element.


The pop() method: remove the specified position

You can remove the elements using the pop() method.

The pop() method removes the element using indexing.

For example,

Output:

In the above example, you can see that the pop() method removed the element using an index.

This shows that you can remove elements using the index, not need elements.

That’s the proof of the list’s ordered, mutable and changeable behaviour.

Delete the element in python

You can remove the element using the del keyword.

The syntax is:
del variable[index]

For example,

Output:

In the above example, you can see that I have removed the element using the del keyword with the index number.

The clear() method

Instantly, you have come up with the strong idea that you don’t want any element in the list, but need the list.

You want a clear list.

How can you do?

Probably, you use pop() or remove() method. But it’s a crappy and boring method.

You can do it with the help of the clear() method.

For example,

Output:


Not so good for secure data!

You had seen list has changeable behaviour that makes it not so secure.

Anybody can change the data. But it is good for update the data.


Make a loop of list

Image credit: edureka

It’s a solid method for coming up with a sequence of list items.

In this, I’m going to show you how can you come up with a sequence of list data.

For example,

Output:

In the above example, you can see that I have come up with the sequence of list elements.

It’s proof that you can come up with the loop of the elements.


Why so good?

If you found the analogy then python list can be used in an open-source platform where data is changing and modifying.

Also, For example, Wikipedia is written in python then the python list can be used. Because Wikipedia is changing its information.


Conclusion

Python list is good for an update the data. But bad for secure data.

You can change the items in the list using the remove and add method.

You can come up with the loop of the list items using for loop.

So, what do you think about the python list? Do you have any tips and tricks you want to show? Comment on it!


For more unique posts see the below box:



I believe that

"Quality brings clarity"

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top