Lists:
Lists:
List is a data type of Python that enables a programmer to store multiple elements together. These elements can be either of similar type or of dissimilar type. A list is written as a collection of comma separated values packed inside of a pair of square brackets.
Consider the following lists:
[] # An empty list
[10, 12, 15, 43] # A list of numbers (all integers)
[10, 12.67, 1.52, 43] # A list of numbers (integers and floats)
[3.87, 67.59, 21.08, 7.5] # A list of numbers (all floats)
['Apple', 'Mango', 'Banana', 'Grapes'] # A list of strings
['Ravi', 'Gorakhpur', 12, 78.2] # A list containing a record of a student
Lists were included as a data type in Python with a goal to let the programmer handle a complete record related to an entity. For example, if we think of the entire record of a student, there may be his name, his father's name, his address, his roll number, his class, his marks etc. Handling all this data using variables is impractical, especially when there is a significant number of students. Lists can hold all the different data that relates to a student very conveniently.
Lists are mutable in nature. That means we can change either a specific or a group of items in a list with new items. This happens due to the peculiar memory management of variables in Python.
Creating lists:
In Python lists are created using any of the following methods:
- Using square brackets
- Using list() constructor
- Using list comprehension
Using square brackets:
Lists can be created by assigning some items enclosed in a square bracket to a name. The list can either be empty or can be having some items. Consider the syntax below:
l1 = [] # Creating an empty list l1.
l2 = [12,67,30] # Creating a list l2 with 3 specific data items.
l3 = input("Enter values to be added in a list: ")
# Creating a list l3 with data items entered by user. Important is to keep all values comma separated.
Using list() constructor:
In Python, list() is a library function that can be used to create a list. Consider the syntax below:
l1 = list() # Creating an empty list l1.
The list() function is designed to work with many arguments like a string, another list, a tuple or even a user input as well. Consider the syntaxes below:
l1 = list('apple') # Creating a list l1 having values: ['a','p','p','l','e'].
l1 = [12,34,56]
l2 = list(l1) # Creating a list l2 by getting 3 specific data items from another list l1.
t1 = (12,67,30)
l1 = list(t1) # Creating a list l1 by getting 3 specific data items from a tuple t1.
l1 = list(input("Enter values to be added in a list: "))
# Creating a list l3 with data items entered by user.
Important:
Creating a list through the last method given above creates a list of strings. This is because we know that default return type of input() function is strings. In order to create a list of numbers, we need to call input function within eval() function. Consider the syntax:
l1 = list(eval(input("Enter values to be added in a list: ")))
# Creating a list l1 of numbers with data items entered by user.
using list comprehension:
# Creating a list using list comprehension
my_list = [x for x in range(5)] # Output: [0, 1, 2, 3, 4]
Major Operations on Lists
Accessing Elements
# Accessing elements by index
my_list = [1, 2, 3]
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 3
Slicing
# Slicing a list
my_list = [1, 2, 3, 4, 5]
print(my_list[1:3]) # Output: [2, 3]
print(my_list[:3]) # Output: [1, 2, 3]
print(my_list[3:]) # Output: [4, 5]
Appending Elements
# Appending an element to the end of the list
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
Inserting Elements
# Inserting an element at a specific position
my_list = [1, 2, 3]
my_list.insert(1, 1.5)
print(my_list) # Output: [1, 1.5, 2, 3]
Extending a List
# Extending a list with another list
my_list = [1, 2]
my_list.extend([3, 4])
print(my_list) # Output: [1, 2, 3, 4]
Concatenation
# Concatenating lists
list1 = [1, 2]
list2 = [3, 4]
result = list1 + list2
print(result) # Output: [1, 2, 3, 4]
Repetition
# Repeating a list
my_list = [1, 2]
result = my_list * 3
print(result) # Output: [1, 2, 1, 2, 1, 2]
Checking Membership
# Checking if an element exists in a list
my_list = [1, 2, 3]
print(2 in my_list) # Output: True
print(4 in my_list) # Output: False
Iterating Through a List
# Iterating through a list
my_list = [1, 2, 3]
for item in my_list:
print(item)
List Methods
Count
# Counting occurrences of an element in a list
my_list = [1, 2, 2, 3]
print(my_list.count(2)) # Output: 2
Index
# Finding the index of an element in a list
my_list = [1, 2, 3]
print(my_list.index(2)) # Output: 1
Remove
# Removing the first occurrence of an element
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list) # Output: [1, 3, 2]
Pop
# Removing an element by index and returning it
my_list = [1, 2, 3]
element = my_list.pop(1)
print(element) # Output: 2
print(my_list) # Output: [1, 3]
Reverse
# Reversing the list in place
my_list = [1, 2, 3]
my_list.reverse()
print(my_list) # Output: [3, 2, 1]
Sort
# Sorting the list in place
my_list = [3, 1, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3]
Example
Here is a complete example demonstrating various operations on lists:
# Creating lists
empty_list = []
my_list = [1, 2, 3, 4, 5]
# Accessing elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 5
# Slicing
print(my_list[1:3]) # Output: [2, 3]
# Appending
my_list.append(6)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
# Inserting
my_list.insert(1, 1.5)
print(my_list) # Output: [1, 1.5, 2, 3, 4, 5, 6]
# Extending
my_list.extend([7, 8])
print(my_list) # Output: [1, 1.5, 2, 3, 4, 5, 6, 7, 8]
# Concatenation
list1 = [1, 2]
list2 = [3, 4]
result = list1 + list2
print(result) # Output: [1, 2, 3, 4]
# Repetition
result = my_list * 2
print(result) # Output: [1, 1.5, 2, 3, 4, 5, 6, 7, 8, 1, 1.5, 2, 3, 4, 5, 6, 7, 8]
# Checking membership
print(2 in my_list) # Output: True
print(9 in my_list) # Output: False
# Iterating through a list
for item in my_list:
print(item)
# List methods
print(my_list.count(2)) # Output: 1
print(my_list.index(3)) # Output: 3
my_list.remove(1.5)
print(my_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
element = my_list.pop(0)
print(element) # Output: 1
print(my_list) # Output: [2, 3, 4, 5, 6, 7, 8]
my_list.reverse()
print(my_list) # Output: [8, 7, 6, 5, 4, 3, 2]
my_list.sort()
print(my_list) # Output: [2, 3, 4, 5, 6, 7, 8]