Python lists are one of the most important data types used in Python Programming. You will use this data type to list different items of something. For example, with lists, you can create a string list which contains different animals, brands or film characters. Or you can create a list of numbers. Whichever it is, python lists will be always with you during your programming activities.
A Python List is created with a Square Brackets . The items belong to this list is added into these square brackets with commas (,) . And it is assigned to a variable. This variable is a list variable. Below, you can find a basic Python List. Here, a is a list.
a = [1, 2, 3]
Here, we will cover Python Lists generally. To learn more about lists of Python, you can check the below, common Python Lessons.
There are different methods of python. With these methods, you can do anything with your python list items. You can add another item to the list, you can remove it, you can sort your item, you can check the length of these lists. There will be different aims of these list activities. To understand Pyhton lists, let’s give more examples.
Below, we will create three lists. The first one is a string list, the second one is a number list and the last one is a list consist of True and False values.
The output of this code will be:
Here, list1, list2 and list3 are the names of the lists. We do not need to use list keyword here. A list can be anything. For example, we can use a, b, c instead of list1, list2 and list3.
As you can see above, we have created specific lists with specific data types. We can also create a mix list consist of both numbers, strings and True/False values. Below, you will find an example of such a Python List.
The output of this python code will be:
The above result is a list. How can we check its type? Here, we can use “type” method to print the type of our list. Here, its type is “list”.
The output will be:
As you can see above, our type is a list.
To create list, we have used square brackets will now. But there is also another way to create a Python list. To do this, we will use list method and we will add our list items inside the curly brackets of list method with another curly brackets. Below, you will find an example of this type of Python list creation.
The output of this list is:
Or, we can create a list with number and list method.
We have learned how to create a Python List. Beside this, there are a lot of Python List Methods that helps us about list related coding activities. Let’s give some examples to these useful python list methods.
The first example will be related to list length. How can you find a Python List Length? To find the length of a python list, we will use, list method.
The output of this code will be 3. Because there are three members of this Python List.
Now, let’s use another method. With this method, we will add another list item to our list. Below, we will add another Elf to our elves list.
As you can see below, out new list will include the new item, Arwen. In real, Arwen is not an item. She is a Hero:)
How about removing an item in a Python List? Below, you can find an example for remove method of python. Here, we will remove 24 from the list.
The output of new list will be:
We can also sort our lists with sort method. To do this let’s give another example. With the below code, we will sort our number list as ascending.
The output of this code will be like below:
As you can see above, the list has sorted as ascending. We can so this also with string list. This time the sorting will be done according to alphabet.
The output of this Python List Example will be:
Here, the list has sorted according to alphabet.
Now, let’s sort these lists in reverse direction with the help of a reverse parameter of python list sort method.
The output of this string list reverse sort will be as descending like below:
Another example is with numbers.
The output of this reverse sort will be:
As you can see this time, the sort has done as descending.
Python Lists are one of the Data Types of Python. There are other Data Types in Python Programming. These are:
Here, you can find also the detailed lessons of these different python data types.
In this List Lesson, we have talked about how to use Python Lists in Python Programming Language. You can learn lists of Python detailly with other lessons.