Python Diaries ….. Day 4

So the list is arrived….

Nishitha Kalathil
4 min readSep 16, 2023

--

In today’s class, we’re going to dive into one of the most versatile data structures in Python: lists. Lists are used to store collections of items, which can be of any data type including numbers, strings, or even other lists. Lists are incredibly versatile and play a crucial role in many Python programs. They allow us to organize and manipulate data in a flexible and powerful way.

Understanding lists is essential for any Python programmer, as they form the backbone of many programs. So, let’s explore the world of lists, learn how to create and manipulate them, and discover the wide range of operations we can perform on them.

You can check my article on Python list exercises for practice here…

Get ready to level up your Python skills! Let’s get started!

Creating Lists

You can create a list in Python by enclosing a sequence of elements within square brackets ([]), separated by commas.

# Example of a list of numbers
numbers = [1, 2, 3, 4, 5]

# Example of a list of strings
fruits = ["apple", "banana", "cherry"]

# Example of a mixed-type list
mixed = [1, "hello", 3.14, True]

Accessing Elements

You can access elements in a list using their index. Indexing starts at 0 for the first element.

numbers = [1, 2, 3, 4, 5]

first_element = numbers[0] # Output: 1
second_element = numbers[1] # Output: 2

List Operations Functions and Methods

Adding Elements

You can add elements to a list using various methods:

# Append: Adds an element to the end of the list
fruits = ["apple", "banana"]
fruits.append("cherry") # fruits is now ["apple", "banana", "cherry"]

# Insert: Adds an element at a specific index
fruits.insert(1, "orange") # fruits is now ["apple", "orange", "banana", "cherry"]

Extending Lists

Appends elements from another list to the end of the current list.

fruits = ["apple", "banana"]
more_fruits = ["cherry", "orange"]
fruits.extend(more_fruits) # fruits is now ["apple", "banana", "cherry", "orange"]

Change Item Value

fruits = ["apple", "banana", "cherry", "banana"]
fruits[1]="melon" # fruits is now ['apple', 'melon', 'cherry', 'banana']

fruits[1:3]=["pappaya","kiwi"] # fruits is now ['apple', 'pappaya', 'kiwi', 'banana']

Removing Elements

You can remove elements from a list using various methods:

# Remove: Removes the first occurrence of a value
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana") # fruits is now ["apple", "cherry", "banana"]

# Pop: Removes and returns the element at a specific index
removed_element = fruits.pop(1) # removed_element is "cherry", fruits is now ["apple", "banana"]

# clear : Removes all elements from the list, making it empty.
fruits = ["apple", "banana", "cherry"]
fruits.clear() # fruits is now []

# del : deleting the elements or entire list
del mylist[0] # deleting the first elemet
del mylist # deleting the entire elemet

Copying Lists

Creates a copy of the list.

fruits = ["apple", "banana", "cherry"]
copy_fruits = fruits.copy() # copy_fruits is a separate copy of fruits
# or
copy_fruits = fruits[:] # Another way to create a copy of the list

Reversing a List

Reverses the order of elements in the list.

numbers = [1, 2, 3, 4, 5]
numbers.reverse() # numbers is now [5, 4, 3, 2, 1]

Sorting a List

Sorts the elements of the list in ascending order (by default).

numbers = [5, 1, 3, 2, 4]
numbers.sort() # numbers is now [1, 2, 3, 4, 5]

# Sorting in descending order
numbers.sort(reverse = True) # numbers is now [5, 4, 3, 2, 1]

Counting

Returns the number of times a specified value occurs in the list.

numbers = [1, 2, 3, 4, 2, 2, 5]
count = numbers.count(2) # count is 3

Click here !!!! You can check my article on Python list exercises for practice

Nested List

A nested list is a list that contains other lists as its elements. This allows you to create a list of lists, providing a way to represent more complex data structures. In Python, you can nest lists to create multi-dimensional data structures.

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In this example, nested_list contains three inner lists, each representing a row of data:

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

Here’s how you can work with a nested list in Python:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Accessing elements
element = nested_list[1][1] # element is 5

# Modifying elements
nested_list[0][2] = 10 # nested_list is now [[1, 2, 10], [4, 5, 6], [7, 8, 9]]

# Adding a new inner list
new_inner_list = [11, 12, 13]
nested_list.append(new_inner_list)

# Removing an inner list
nested_list.remove([4, 5, 6])

# Displaying the nested list
for inner_list in nested_list:
print(inner_list)

Output:

[1, 2, 10]
[7, 8, 9]
[11, 12, 13]

Today, we’ve delved into the world of lists in Python. You’ve learned how to create lists, access their elements, and perform various operations on them. Lists are a powerful tool for organizing and manipulating data, and they’ll be an essential part of your Python toolkit.

Click here !!!! You can check my article on Python list exercises for practice

You can access the other topics in this tutorial series right here:

In the next class, we’ll explore more advanced data structures like tuples and dictionaries. Keep practicing and happy coding!

--

--