Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

List Program in Python

In this article, we will understand and discuss lists in python. Also we will deep dive and perform different operations on the python list. Let’s discuss the list now.

List in Python

A list is a grouping of related or unrelated items of data. For example,

Let’s say we have to note the age of five students. We may just build a list instead of 5 different variables:

Create a Python List

Python allows you to create lists by enclosing elements in [] and separating them with commas. For example,

Python Program of Creating List

Sample Description
numbers = [1, 2, 3, 4]

print(numbers)

Output

[1, 2, 3, 4]

Here, we have created a list named numbers with 4 integer items.

There can be any number of items in a list, and they can be of any type (integer, float, string, etc.). For example,

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]

Access Python List Elements

Each item in a list has a corresponding number in Python. The number is referred to as a list index.

The index number (0, 1, 2…) can be used to retrieve elements of an array. For example,

Python Program to Access List Elements

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++

In the example above, we made a list named languages.

We can see that each item on the list is linked to an index number in this instance. Additionally, we accessed the items using the index number.
Recall that the list index always begins at 0. Consequently, index 0 instead of index 1 contains the first element in a list.

Negative Indexing in Python

Python allows negative indexing for its sequences. The last item is represented by the index of -1, the second last by -2, and so on.

Negative Indexing Program in Python

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[-1])   # C++

# access item at index 2
print(languages[-3])   # Python

Output

C++
Python

Note: Python raises the IndexError exception if the supplied index does not exist in the list.

Slicing of a Python List

The slicing operator in Python allows you to access a subset of the list’s items rather than just a single one. For example,

Program of Slicing of a Python List

# List slicing in Python

my_list = ['p','r','e','p','b','y','t','e','s']

# items from index 2 to index 4
print(my_list[2:5])

# items from index 5 to end
print(my_list[5:])

# items beginning to end
print(my_list[:])

Output

['e', 'p', 'b']
['y', 't', 'e', 's']
['p', 'r', 'e', 'p', 'b', 'y', 't', 'e', 's']

Here,
my_list[2:5] returns a list with items from index 2 to index 4.
my_list[5:] returns a list with items from index 1 to the end.
my_list[:] returns all list items.

Note: The start index is inclusive but the end index is exclusive when we slice lists.

Add Elements to a Python List

There are several ways to add items to a list using Python List.

Using append()

The list’s last will be the position to add items using the append() method.

list program in python 6

numbers = [20, 30, 50, 10]

print("Before Append:", numbers)

# using append method
numbers.append(32)

print("After Append:", numbers)

Output

Before Append: [20, 30, 50, 10]
After Append: [20, 30, 50, 10, 32]

In the example above, we made a list named numbers. Notice the line,
numbers.append(32)
In this case, append() extends the array by 32.

Using extend()

To add every item from one list to another, we use the extend() method. For example,

prime_numbers = [2, 3, 5]
print("List1:", prime_numbers)

even_numbers = [4, 6, 8]
print("List2:", even_numbers)

prime_numbers.extend(even_numbers)

print("List after append:", prime_numbers)

Output

List1: [2, 3, 5]
List2: [4, 6, 8]
List after append: [2, 3, 5, 4, 6, 8]

prime_numbers and even_numbers are two lists in the example above. Notice the statement,

prime_numbers.extend(even_numbers)
Here, we are adding all of the even_numbers components to the prime_numbers.

Change List Items in Python

Python lists are mutable. Meaning lists are changeable. Additionally, we can alter list elements by assigning new values with the = operator.

Python Program to Change List Items in Python

languages = ['Python', 'Swift', 'C++']

# changing the third item to 'C'
languages[2] = 'C'

print(languages)  # ['Python', 'Swift', 'C']

Output

['Python', 'Swift', 'C']

The value at index 3 in this case is initially "C++." The value was then changed to "C" using

languages[2] = ‘C’

Remove an Item From a List

There are several ways to remove items to a list using Python List.

Using del()

The del statement in Python can be used to remove one or more items from a list. For example,

languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the second item
del languages[1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the last item
del languages[-1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust']

# delete first two items
del languages[0 : 2]  # ['C', 'Java', 'Rust']
print(languages)

Output

['Python', 'C++', 'C', 'Java', 'Rust', 'R']
['Python', 'C++', 'C', 'Java', 'Rust']
['C', 'Java', 'Rust']

Using remove()

The remove() method can be used to remove a list item as well. For example,

languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# remove 'Python' from the list
languages.remove('Python')

print(languages) # ['Swift', 'C++', 'C', 'Java', 'Rust', 'R']

Output

['Swift', 'C++', 'C', 'Java', 'Rust', 'R']

Conclusion
We are concluding a list program in python now, but there are lots of methods and stuff to perform on list. Play more with the list in python, after that you will get how powerful the list is in python.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series
Python Loop Program
Anagram Program in Python
Fizzbuzz Program in Python
String Programs in Python

Leave a Reply

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