Python List-Store as much as you want?

In recent days my friend and me storing our name on each new variable. It taking a lot of times to store each one of the names in a new variable.

So, we decided to move to something easy and flexible. Our friend group is aware of the python list.

We had used a python list to store each of the names in one variable. It’s great!

You have finally found it. You can store multiple items in one variable. No need to create more variables.

The misconception is that every new variable is clear and concise. It’s true but in the Python list, we can do it easily.

So you are going to see how to store multiple items at once.

First, you will see what’s the list?

Why we’re using it?

What’s the benefit?

How to do it?

List

Python List is the ordered group of items store in one variable.

The list can be of any data type or mixed data type. You don’t care about the data type in the list.

Creation of list

We can create a list using square brackets.

The square brackets hold the multiple items of the list. It indicates by [].

List items

List Items are ordered, changeable and allow duplicate items in the list.

Ordered

List items are ordered. You can use [] to check the order of list.

The first items start with [0]. The number inside [] brackets is called indexing. Indexing gives the particular item to you.

Changeable

You can change the item with the remove, append method In the list.

Allow duplicate items

The python list allows for duplicate items. Duplicate items are allowed because of different index number.

List Length- To Find Number Of Items

Suppose you have to find the number of items in the list. The traditional method is to count every item.

But If there are thousands of items. So it will take more time to count. To make it easy you use the len() method in the list.

List items – Data types

The data type of list items can be mixed. In one list there are many different data types.

Range of Indexes

The search starts from the first given number to the second number. It uses [start:end].

The return value is a new list. In the list some items of the old list are present.

The indexing always starts from 0.

The element at index 0 is the first item.

Range of Negative Indexes

If you want to search items from the last of, then you can use negative indexing.

The last element index in negative indexing is -1.

The last number is not included. In this case, -1 has not included.

The last number would be last-1.

Here, it is -1-1=-2.

type() method in list:

In python list considered as an object. Suppose you have to find the data type of list, you can use the type() method.

List Constructor- Creating a list

You can also create a list without [] brackets with a list() constructor.

Check if items exist

To find if the item is present in the list we use in keyword.

if present it will print a message.

Conclusion

Python list is ordered, changeable and allow duplicate items. The data type of list is <class ‘list’>. You can use a python list to store the school registration form of students. You have not to create a separate variable for each student. We can access a particular item in the list with the help of an index number.

See also:

PythonOperators

https://vipulkunwar503.code.blog/2021/06/19/python-operators/

PythonSyntax

https://vipulkunwar503.code.blog/2021/06/13/python-syntax/

Leave a comment