Python Lists

Python List

A list in Python is a mutable, ordered collection of items that can store elements of different data types. Lists are one of the most versatile data structures in Python, used to store multiple items in a single variable. Lists are determined by square brackets [ ]

Example:

# Examples of list declarations in Python
list1 = [] # An empty list
list2 = [4, 5, 1, 7, 3] # A list with 5 integers
list3 = [True, False, True, False, True] # A list with 5 boolean values
list4 = [1, ‘XYZ’, True] #Different types of values can be stored in list

Explanation:

● list1 = [ ]: This is an empty list that doesn’t contain any elements.
● list2 = [4, 5, 1, 7, 3]: This list contains 5 integer values: 4, 5, 1, 7, and 3.
● list3 = [True, False, True, False, True]: This list contains 5 boolean values: True, False, True, False, and True.
● list4 = [1, ‘XYZ, ‘True’]: This list contains 3 values of distinct data types. In python it is possible to store different types of values.

Creating and Accessing a Python List:
Scenario: Let’s create a list with the values {2, 4, 10, 5, 15, 3} and access the 3rd element using the list index.

# Create a list and access the 3rd element
numbers = [2, 4, 10, 5, 15, 3]
print(f”The third element: {numbers[2]}”)

Explanation:

numbers = [2, 4, 10, 5, 15, 3]: This initializes a list with 6 integer values: 2, 4, 10, 5, 15, and 3.
print(f”The third element: {numbers[2]}”): This prints the 3rd element of the list. In Python, lists are zero-indexed, meaning the index starts from 0. Therefore, numbers[2] refers to the 3rd element, which is 10.

Example: Create a list with student name, roll no and class

student = [“Jannat”, 44, ‘VIII’]

print(student) #Output: [“Jannat”, 44, ‘VIII’]

Modifying the list
1. Negative Indexing:
    Negative indexing allows you to access elements from the end of the list.

my_list = [1, 2, 3, ‘apple’, ‘banana’]

print(my_list[-1]) # Output: ‘banana’
print(my_list[-2]) # Output: ‘apple’

Explanation:
-1 will return the last element of the list.
-2 will return second last element of list
-3 will return the 3rd last element of the list and so on.

2. Slicing:
You can get a range of elements using slicing.

my_list = [1, 2, 3, ‘apple’, ‘banana’]
print(my_list[1:4]) # Output: [2, 3, ‘apple’]
print(my_list[:3]) # Output: [1, 2, 3]

Explanation:
my_list[1:4]: This will cut the list from index 1 till index 3, it will not include index 4
my_list[:3]: This will cut the list from start till index 2, it will not include index 3

3. Changing Elements:
You can modify list elements by assigning new values.

my_list = [1, 2, 3, ‘apple’, ‘banana’]
my_list[1] = ‘orange’
print(my_list) # Output: [1, ‘orange’, 3, ‘apple’, ‘banana’]

Explanation:
It will replace the element at index 1 with ‘orange’

4. Appending Elements:
You can add new elements to the end of a list using append().

my_list = [1, ‘orange’, 3, ‘apple’, ‘banana’]
my_list.append(‘grape’)
print(my_list)
# Output: [1, ‘orange’, 3, ‘apple’, ‘banana’, ‘grape’]

Explanation: It will add the element at the end of the list

5. Inserting Elements:
To insert an element at a specific position, use insert().

my_list = [1, ‘orange’, 3, ‘apple’, ‘banana’, ‘grape’]
my_list.insert(2, ‘cherry’)
print(my_list)
# Output: [1, ‘orange’, ‘cherry’, 3, ‘apple’, ‘banana’, ‘grape’]

Explanation: The first parameter is the index where the element is to be inserted and second parameter is the element to be inserted. This will insert the element at mentioned index

6. Removing Elements:
● remove(): Removes the first occurrence of a specific value.
pop(): Removes and returns the element at a given index (default is the last).
del: Deletes elements or slices of a list.

my_list = [1, ‘orange’, ‘cherry’, 3, ‘apple’, ‘banana’, ‘grape’]

my_list.remove(‘apple’)
print(my_list)
# Output: [1, ‘orange’, ‘cherry’, 3, ‘banana’, ‘grape’]

my_list.pop(1)
print(my_list)
# Output: [1, ‘cherry’, 3, ‘banana’, ‘grape’]

del my_list[2]
print(my_list)
# Output: [1, ‘cherry’, ‘banana’, ‘grape’]

7. Extending a List:
To add all elements from one list to another list, use extend().

my_list = [1, ‘cherry’, ‘banana’, ‘grape’]
new_list = [‘watermelon’, ‘kiwi’]
my_list.extend(new_list)
print(my_list)
# Output: [1, ‘cherry’, ‘banana’, ‘grape’, ‘watermelon’, ‘kiwi’]

Explanation:
We’re adding elements from new_list to my_list. It is different from append() as append only adds an element and it adds all the elements of a list.

8. Sorting a List:
You can sort lists with sort() function.

num_list = [5, 2, 9, 1]
num_list.sort()
print(num_list) # Output: [1, 2, 5, 9]

Explanation: It sorts the list in ascending order.

9. Reversing a List:
Use reverse() to reverse the elements of a list.

my_list = [1, ‘cherry’, ‘banana’, ‘grape’, ‘watermelon’, ‘kiwi’]
my_list.reverse()
print(my_list)
# Output: [‘kiwi’, ‘watermelon’, ‘grape’, ‘banana’, ‘cherry’, 1]

Explanation:
It reverses the order of elements of the list.

10. List Length:
You can find the length of a list with len().

my_list = [‘kiwi’, ‘watermelon’, ‘grape’, ‘banana’, ‘cherry’, 1]
print(len(my_list)) # Output: 6

Explanation:
It returns the number of elements in the list

11. Copying a List:
Use copy() to create a copy of a list.

my_list = [‘kiwi’, ‘watermelon’, ‘grape’, ‘banana’, ‘cherry’, 1]
copy_list = my_list.copy()
print(copy_list)
# Output: [‘kiwi’, ‘watermelon’, ‘grape’, ‘banana’, ‘cherry’, 1]

Explanation:
It creates a copy of the list.

Multidimensional List:
Multidimensional list is basically lists with list i.e. collection of list.

Example:

list2 = [[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]] # A multidimensional list with 9 integers

employees = [
    [1,’John’,1000],
    [2,’Jane’,2000], #A two-dimensional list containing employee data
    [3,’Joe’,4000]
]

#modifying 2d list employee
#changing Joe’s Salary to 6000
employees[2][2] = 6000
print(employees)
#Output: [[1, ‘John’, 1000], [2, ‘Jane’, 2000], [3, ‘Joe’, 6000]]

# Modifying employee 2’s Name to ‘Jack’
employees[1][1] = ‘Jack’
print(employees)
#Output: [[1, ‘John’, 1000], [2, ‘Jack’, 2000], [3, ‘Joe’, 6000]]

We will learn how to access it while learning loops.

Course Video

Course Video English:

Task

1. Integer List:
Create a List of integers with values from 1 to 5.
Print all elements of the List.

2. String List:
Create a List of strings with the names of three fruits.
Print all elements of the List.

3. Double List:
Create a List of doubles with any 4 double values.
Print the sum of all elements in the List.

4. Boolean List:
Create a List of booleans with alternating true and false values.
Print all elements of the List.

5. Create a list of voters id consisting of aadhar no., name, age and location.
Example: [121234345656, “Jannat”, 22, “Mumbai”]

Task Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

Lists in Python are ordered, mutable collections that can store multiple items, including mixed data types, in a single variable.

Python lists are used to store and organize multiple items in a single variable, enabling easy access and manipulation of the data.

Lists are mutable (modifiable), ordered (elements retain their sequence), and allow storing elements of different data types.

The slice [-1:] in Python retrieves the last element of a list, as negative indexing starts from the end.

Common list methods include append() (add an element), remove() (delete an element), and pop() (remove and return an element).

The get() method is used to retrieve values from dictionaries by key and is not a method available for Python lists.

\List operations include indexing, slicing, concatenation, appending elements, and removing or modifying elements.

The list type in Python is represented by the list class, and it supports dynamic arrays that can grow or shrink as needed.

A list can be initialized using square brackets, e.g., my_list = [1, 2, 3], or the list() constructor, e.g., my_list = list(range(5)).

The get() method is used to retrieve values from dictionaries by key and is not a method available for Python lists.

 

 
4o