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.
first_list = [1,2,3,4,5]
print(first_list)
List having a string, integer, float, and boolean. Means list is Heterogenous.
second_list = ['string',1,13.3,True]
print(second_list)
Characteristics of a List
Let us look at the characteristics of the list one by one:
Characteristic | Meaning |
---|---|
Mutable | This means that we can change the items stored within the list. |
Duplicate Elements | Duplicate items are allowed in the List. |
Ordered | Lists maintain the order of the items in which they are inserted. |
Heterogeneous | Lists can store elements of various data types. |
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:
emp_details = ['Brad','Lee','Sales',380,250000,True]
print(emp_details)
We have successfully changed the employee name at index 0.
emp_details[0] = 'Peter'
Now name at location 0 is ‘Peter’ instead of ‘Brad’.
print(emp_details)
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.
duplicates = ['string',1,1,1,1,1]
print(duplicates)
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.
lst1 = [1,2,3]
lst2 = [1,2,3]
print(lst1 == lst2)
On changing order of the elements. Now list are not same.
lst1 = [1,2,3]
lst2 = [1,3,2]
print(lst1 == lst2)
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.
my_list = list(('string',1,13.3,True))
print(my_list)
Convert a list to tuple.
my_tuple = ('string',1,13.3,True)
my_list = list(my_tuple)
print(my_list)
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.
emp_details = ['Brad','Lee','Sales',380,250000,True]
print(type(emp_details))
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:
names = input("Please enter names (separated by space)")
names_list = names.split()
print(names_list)
Join a List
In Python, there are several ways by which we can concatenate the items of the two or more lists:
1. Simply using the + operator.
numbers1 = [1,2,3]
numbers2 = [4,5,6]
result = numbers1 + numbers2
print(result)
2. Using the extend() method, which is used to add elements from one list to others.
numbers1 = [1,2,3]
numbers2 = [4,5,6]
numbers1.extend(numbers2)
print(numbers1)
3. Using the append() method. When joining a list using the append method, we have to iterate over the elements of the list.
numbers1 = [1,2,3]
numbers2 = [4,5,6]
for n in numbers2:
numbers1.append(n)
print(numbers1)
Membership Test
Membership operators in and not in are used to check whether an item is present in the list or not.
name = ['Logical' ,'Python']
print('Logical' in name)
print('Logical' not in name)
Length of List
The len() function is used to check the length of the list.
my_list = [1,2,3,4,5]
print(len(my_list))