Python Lists


Introduction to Python Lists

In Python, list is one of the built-in collection/container data types. Lists are used to store multiple data at once.

Lists are created in python using the square brackets [ ]. Each element in the list is separated by a comma. In the list, we can hold any object type like string, boolean, float, or even list inside the list. We can also use the list() function for creating the list. Let’s have a look at a few examples:

List with just integers.

In [1]:
first_list = [1,2,3,4,5]
print(first_list)
[1, 2, 3, 4, 5]

List having a string, integer, float, and boolean. Means list is Heterogenous.

In [2]:
second_list = ['string',1,13.3,True]
print(second_list)
['string', 1, 13.3, True]

Characteristics of a List

Let us look at the characteristics of the list one by one:

CharacteristicMeaning
MutableThis means that we can change the items stored within the list.
Duplicate ElementsDuplicate items are allowed in the List.
OrderedLists maintain the order of the items in which they are inserted.
HeterogeneousLists can store elements of various data types.
Python Lists

Mutable – Changeable

Unlike strings, list is mutable. We can change an item at any index position at any time.

Please take a look at an example below:

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
print(emp_details)
['Brad', 'Lee', 'Sales', 380, 250000, True]

We have successfully changed the employee name at index 0.

In [2]:
emp_details[0] = 'Peter'

Now name at location 0 is ‘Peter’ instead of ‘Brad’.

In [3]:
print(emp_details)
['Peter', 'Lee', 'Sales', 380, 250000, True]

Duplicate Elements

In a list, we can have any number of duplicate elements, Python has no issue with that.

Duplicates are allowed in the list.

In [1]:
duplicates = ['string',1,1,1,1,1]
print(duplicates)
['string', 1, 1, 1, 1, 1]

Ordered

In Python, a list is an ordered sequence. By ordered sequence, we mean, items are stored in the same order in which they are inserted.

Check if list is ordered. Both list contains same elements, so both list are same.

In [1]:
lst1 = [1,2,3]
lst2 = [1,2,3]
print(lst1 == lst2)
True

On changing order of the elements. Now list are not same.

In [2]:
lst1 = [1,2,3]
lst2 = [1,3,2]
print(lst1 == lst2)
False

Using list() Constructor

  • We can also use the list() constructor to create the python list. We have to pass comma separated items in the parenthesis.
  • This list() function can also be used for the type conversion to the list. For example, if we want to convert a tuple to the list or the set to the list.

List Constructor to create a list.

In [1]:
my_list = list(('string',1,13.3,True))
print(my_list)
['string', 1, 13.3, True]

Convert a list to tuple.

In [2]:
my_tuple = ('string',1,13.3,True)
my_list = list(my_tuple)
print(my_list)
['string', 1, 13.3, True]

type() function

At any stage in our program, we may need to check whether the variable is a list or not, we can use the type() function for this purpose. If the type of the variable is a list, then the type() function will return the ‘list’ keyword.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
print(type(emp_details))
<class 'list'>

Talking input in form of List

If we need to take input from the user in the form of a list, then there is no direct way for this in Python. So, to achieve this, we have to use the split method to convert the string to a list. Let us look in the example below:

In [1]:
names = input("Please enter names (separated by space)")
names_list = names.split()
print(names_list)
Please enter names (separated by space)Steve Bill
['Steve', 'Bill']

Join a List

In Python, there are several ways by which we can concatenate the items of the two or more lists:

Join a List

1. Simply using the + operator.

In [1]:
numbers1 = [1,2,3] 
numbers2 = [4,5,6] 

result =  numbers1 + numbers2

print(result)
[1, 2, 3, 4, 5, 6]

2. Using the extend() method, which is used to add elements from one list to others.

In [1]:
numbers1 = [1,2,3] 
numbers2 = [4,5,6] 

numbers1.extend(numbers2)

print(numbers1)
[1, 2, 3, 4, 5, 6]

3. Using the append() method. When joining a list using the append method, we have to iterate over the elements of the list.

In [1]:
numbers1 = [1,2,3] 
numbers2 = [4,5,6] 

for n in numbers2:
    numbers1.append(n)

print(numbers1)
[1, 2, 3, 4, 5, 6]

Membership Test

Membership operators in and not in are used to check whether an item is present in the list or not.

In [1]:
name = ['Logical' ,'Python']

print('Logical' in name)
print('Logical' not in name)
True
False

Length of List

The len() function is used to check the length of the list.

In [1]:
my_list = [1,2,3,4,5]
print(len(my_list))
5

Resources

Handwritten NotesHandwritten Notes - List Characteristics

References

  1. Python Lists
  2. Characteristics of Lists
  3. Python List Examples