Python

Python List With Illustrations – Detailed Answers

Python lists will be covered in this guide. A list is a data type that enables the storage of numerous kinds of data. The fact that a list is a compound data type allows you to store objects of several data types within it, such as integer, float, and string.

1. Python List creation

Let’s examine the Python list creation process. Simply include each item in a square bracket [] and separate them with a comma to make a list.

# list of floats
num_list = [11.22, 9.9, 78.34, 12.0]

# list of int, float and strings
mix_list = [1.13, 2, 5, "beginnersbook", 100, "hi"]

# an empty list
nodata_list = []

A list can include data elements of the same type or distinct types, as we saw previously. List falls within the category of compound data because of this.

2. gaining access to a list’s items

Syntax for getting at list items

list_name[index]

Example:

# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]

# prints 11
print(numbers[0])

# prints 300
print(numbers[5])

# prints 22
print(numbers[1])

Output:

11
300
22

Points to Note:
1. The index cannot be a float number.
For example:

# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]

# error
print(numbers[1.0])

Output:

TypeError: list indices must be integers or slices, not float

To prevent an IndexError, the index must be within the range. If the index of a list with 10 members is outside of the range of 0 to 9, we will receive an IndexError. However, in some circumstances, going below 0 wouldn’t be a problem; we’ll talk about that in the section after this one.

For example:

# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]

# error
print(numbers[6])

Output:

IndexError: list index out of range

3. Access the list elements from the end using the negative index

In contrast to other programming languages, Python permits the use of negative indices without any problems. The purpose of this is to provide you access to the list’s elements beginning at the end. An index of -1, for instance, would access the last member in the list; -2, the next last; -3, the third last; and so on.

3.1 Python Negative Index Example

# a list of strings
my_list = ["hello", "world", "hi", "bye"]

# prints "bye"
print(my_list[-1])

# prints "world"
print(my_list[-3])

# prints "hello"
print(my_list[-4])

Output:

bye
world
hello

4. Slicing in Python to obtain a sublist

In Python, we may use the slicing process to obtain a sublist from a list. If our list n list has 10 elements, for example, we can use the colon: operator to cut it into smaller pieces. To further grasp this, let’s look at an example:

4.1 Slice illustration

# list of numbers
n_list = [1, 2, 3, 4, 5, 6, 7]

# list items from 2nd to 3rd
print(n_list[1:3])

# list items from beginning to 3rd
print(n_list[:3])

# list items from 4th to end of list
print(n_list[3:])

# Whole list
print(n_list[:])

Output:

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

5. List operations, no.

We can conduct a number of operations on Lists.

5.1 Inclusion

A list can have elements added in a variety of ways.

# list of numbers
n_list = [1, 2, 3, 4]

# 1. adding item at the desired location
# adding element 100 at the fourth location
n_list.insert(3, 100)

# list: [1, 2, 3, 100, 4]
print(n_list)

# 2. adding element at the end of the list
n_list.append(99)

# list: [1, 2, 3, 100, 4, 99]
print(n_list)

# 3. adding several elements at the end of list
# the following statement can also be written like this:
# n_list + [11, 22]
n_list.extend([11, 22])

# list: [1, 2, 3, 100, 4, 99, 11, 22]
print(n_list)

Output:

[1, 2, 3, 100, 4]
[1, 2, 3, 100, 4, 99]
[1, 2, 3, 100, 4, 99, 11, 22]

5.2 Update components

The values of elements in a List can be changed. To further grasp this, let’s look at an example:

# list of numbers
n_list = [1, 2, 3, 4]

# Changing the value of 3rd item
n_list[2] = 100

# list: [1, 2, 100, 4]
print(n_list)

# Changing the values of 2nd to fourth items
n_list[1:4] = [11, 22, 33]

# list: [1, 11, 22, 33]
print(n_list)

Output:

[1, 2, 100, 4]
[1, 11, 22, 33]

5.3 Delete elements

# list of numbers
n_list = [1, 2, 3, 4, 5, 6]

# Deleting 2nd element
del n_list[1]

# list: [1, 3, 4, 5, 6]
print(n_list)

# Deleting elements from 3rd to 4th
del n_list[2:4]

# list: [1, 3, 6]
print(n_list)

# Deleting the whole list
del n_list

Output:

[1, 3, 4, 5, 6]
[1, 3, 6]

5.4 Deleting elements using remove(), pop() and clear() methods

remove(item): Removes specified item from list.
pop(index): Removes the element from the given index.
pop(): Removes the last element.
clear(): Removes all the elements from the list.

# list of chars
ch_list = ['A', 'F', 'B', 'Z', 'O', 'L']

# Deleting the element with value 'B'
ch_list.remove('B')

# list: ['A', 'F', 'Z', 'O', 'L']
print(ch_list)

# Deleting 2nd element
ch_list.pop(1)

# list: ['A', 'Z', 'O', 'L']
print(ch_list)

# Deleting all the elements
ch_list.clear()

# list: []
print(ch_list)

Output:

['A', 'F', 'Z', 'O', 'L']
['A', 'Z', 'O', 'L']
[]

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button