Lists in Python | Part 1
Python offers a range of data types referred to as sequences. In this article today, we will be learning about lists in Python. We will be defining what a list is and learn different ways of creating lists. Thereafter, we will also be learning different ways of manipulating lists in Python. We will also learn to delete the lists. This is the first article out of two. To learn more about lists please refer the article Lists in Python | Part 2
What are Lists in Python?
Lists are a collection of homogeneous or nonhomogeneous objects. Therefore, the elements of a list need not be of the same datatype. They are the most versatile data type. A single list can contain a variety of data types like integers, strings as well as lists themselves.
Unlike strings, lists are mutable. Therefore, they can be modified even after creation. Hence, we will be learning to add and remove elements from lists.
Creating Lists in Python:
We can create lists in Python by assigning a pair of square brackets to a variable. The elements are placed inside the square brackets. As a list can be a collection of nonhomogeneous elements, we can add elements of any data type to a list.
Note: A list can also contain duplicate elements. We can also add lists into another list.
creation of lists:
# Creating a blank list list = [] print(list) print() # Creating list with one element list = ['Hello'] print(list) print() # Creating list with multiple elements list = ['Hello', 'World', 'Program'] print(list) print() # Creating a nested list list = [['Hello World'], ['Program']] print(list) print() # List with duplicate elements list = [1, 2, 3, 4, 5, 5, 1] print(list) print() # List with distinct elements list = ['Hello', 1, 'World', 2] print(list)
Output:
[] ['Hello'] ['Hello', 'World', 'Program'] [['Hello World'], ['Program']] [1, 2, 3, 4, 5, 5, 1] ['Hello', 1, 'World', 2]
Adding elements to Lists in Python:
There are a few different methods to add elements to lists. We will be learning the different methods that Python supports to add elements to lists.
1. append(): The append method can add one element to the end of a list. We pass the element to add to the list as the parameter to the append() method. We can pass the value directly as a parameter or store it in a variable and pass the variable. If we want to add multiple elements using append() method we can use a looping statement.
Syntax:
list_name.append(value)
The below code snippet shows adding elements to lists in Python using the append() method:
# Creating a list list1 = [] print(list1) print() # adding single elements to list list1.append('A') list1.append('B') list1.append('C') print(list1) print() # adding elements to list using for loop for i in range(1,3): list1.append(i) print(list1) print() # adding another list to a list list2 = ['a', 'b'] list1.append(list2) print(list1)
Output:
[] ['A', 'B', 'C'] ['A', 'B', 'C', 1, 2] ['A', 'B', 'C', 1, 2, ['a', 'b']]
2. insert(): The insert() method is similar to the append() method. However, unlike the append() method the insert() method allows us to add elements at any desired position within the list. Hence, we pass the index that we want to add the element into as well as the element itself as parameters to the insert() method.
Syntax:
list_name.insert(position, value)
The below code snippet shows adding elements to lists in Python using the insert() method:
# Creating a list list1 = ['A', 'B', 'C', 'D', 'E'] print(list1) print() # adding elements to a list using insert() method list1.insert(0, 'Z') list1.insert(5, 'X') print(list1)
Output:
['A', 'B', 'C', 'D', 'E'] ['Z', 'A', 'B', 'C', 'D', 'X', 'E']
3. extend(): The extend() method is similar to the append() method. It adds the element to the end of the list. The main difference between the extend() method and the append() method is that the extend() method allows us to add multiple elements to the list.
Syntax:
list_name.extend(value1, value2, value3 )
The below code snippet shows adding elements to a list using the extend() method:
# Creating a list list1 = ['A', 'B', 'C', 'D', 'E'] print(list1) print() # adding elements to a list using extend() method list1.extend([1, 2, 3]) print(list1)
Output:
['A', 'B', 'C', 'D', 'E'] ['A', 'B', 'C', 'D', 'E', 1, 2, 3]
This is the end of this article. This is the first part of two posts about lists in Python. We will be learning accessing, removing elements from a list and list slicing in the next article.
Leave a Reply