Python Lists


Python List Basics


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.



Python List Creation


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.


list1 = ["Porche", "Aston Martin", "Ferrari"]
list2 = [3,5,9,16,21,24]
list3 = [True, False, True]


The output of this code will be:


['Porche', 'Aston Martin', 'Ferrari']
[3, 5, 9, 16, 21, 24]
[True, False, True]


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.


mylist = ["Hello", 111, "Merhaba", True, 45, False]


The output of this python code will be:


['Hello', 111, 'Merhaba', True, 45, False]




Type of Python List


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”.


mylist = ["Hello", 111, "Merhaba", True, 45, False]


The output will be:


<class 'list'>


As you can see above, our type is a list.



Another Way To Create Lists


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.


mynewlist = list(("lion", "jaguar", "tiger", "puma"))


The output of this list is:


['lion', 'jaguar', 'tiger', 'puma']


Or, we can create a list with number and list method.


mynewlist = list((1,2,3,4,5))
[1, 2, 3, 4, 5]



Python List Methods


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.


Length of a List

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.


cars = ["Porche", "Aston Martin", "Ferrari"]
length = len(cars)


The output of this code will be 3. Because there are three members of this Python List.





Item Add/Remove


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.


elves = ["Legolas", "Elrond", "Galadriel"]


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:)


['Legolas', 'Elrond', 'Galadriel', 'Arwen']


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.


list = [6, 15, 24, 36, 55]


The output of new list will be:


[6, 15, 36, 55]



Python List Sorting


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.


list = [85, 9, 3, 15, 75, 23]


The output of this code will be like below:


[3, 9, 15, 23, 75, 85]


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.


list = ["cc", "bb", "ee", "dd", "aa"]


The output of this Python List Example will be:


['aa', 'bb', 'cc', 'dd', 'ee']


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.


list = ["cc", "bb", "ee", "dd", "aa"]


The output of this string list reverse sort will be as descending like below:


['ee', 'dd', 'cc', 'bb', 'aa'] 


Another example is with numbers.


list = [85, 9, 3, 15, 75, 23]


The output of this reverse sort will be:


[85, 75, 23, 15, 9, 3]


As you can see this time, the sort has done as descending.



Other Data Types


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.


Back to: Python Programming Course > Python Lists

Leave a Reply

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

Python Programming Course