Python list | Python list programs for absolute beginners

Contents

Python List Update

List is one of the most used data types in Python. A Python list can be easily identified via square brackets [ ]. Lists are used to store data items where each data item is separated by a comma (,). A Python list can have data items from any type of data, either of type integer or boolean.

One of the main reasons that lists are widely used is that lists mudable. Being a mutable medium, any data element in a List can be replaced by any other data element. This makes the Lists different from Tuplas, which are also used to store data items, But they are immutable.

As an example.,

a = ['Hello', 1, 4.63, True, "World", 2.0, "False"]

Indexing in a list they are of two types:

1. Positive indexing: here indexing starts from 0, moving from left to right.

2. Negative indexing: in this, indexing starts from right to left and the rightmost element has an index value of -1.

Taking the above example as a reference, positive and negative indexing will look like this:

Python List Image

Based on item indices, we can also do Slice in lists.

As an example, taking our previously established List a,

a[2 : 5] gives [4.63, True, "World"]
a[ : ] gives ["Hello", 1, 4.63, True, "World", 2.0, "False"]
a[-1: -3 : -1] gives ["False", 2.0]

In the same way, we can replace our existing data element with a new value.

As an example, our list a:

a[1] = 2
print(a) gives ['Hello', 2, 4.63, True, "World", 2.0, "False"]

Apart from these operations, Python List has many functions to meet the needs of anyone. Below are popular Python programs that are never listed and that will help build a beginner's logical mind.

Python List Programs Every Beginner Should Know

1. Program to check if the given list is in ascending order or not

list1 = [1, 2, 3, 5, 4, 8, 7, 9]
temp_list = list1[:]
list1.sort()
if temp_list == list1:
    print("Given List is in Ascending Order")
else:
    print("Given List is not in Ascending Order")
'''
Expected Output:
Given List is not in Ascending Order
'''

Explanation: Given a list list1 having 8 int values. She is ready list1 is assigned to another variable temp_list. As we are using the .sort function () of the list, will sort our list list1 which means it will arrange our list1 data items in ascending order.

Therefore, our original list had to be stored somewhere. Now our temp_list has an older version of the list list1 weather list1 has become the ordered version of list1. Now apply, and the condition in temp_list Y l1. If temp_list is equal to l1, this means that our list was already sorted. If this condition is met, print The given list is in ascending order more print The given list is not in ascending order.

2. Program to find even numbers from a list

list2 = [2, 3, 7, 5, 10, 17, 12, 4, 1, 13]
for i in list2:
    if i % 2 == 0:
        print(i)
'''
Expected Output:
2
10
12
4
'''

Explanation: Given a list list2 having 10 int values. We need to find even numbers from the given list. Use by loop to iterate over the list. And for each iteration, use el yes condition to check if the list data item in a particular iteration is absolutely divisible by 2, meaning i% 2 must be equal to zero. Now, All posts that meet this condition will be printed.

3. Program to merge two lists

list3 = [1, 2, 4, 6]
list4 = [9, 3, 19, 7]
list3.extend(list4)
print(list3)
'''
Expected Output:
[1, 2, 4, 6, 9, 3, 19, 7]
'''

Explanation: List data list3 Y list4 have certain numerical values. Use the .enlarge() python list function to merge both lists. Therefore, merging these lists will give a single list with items from the first list followed by the second list. Now print the list3, which will have items from both lists list3 Y ready 4. If you want the second list list4 the items to be printed first, followed by data items list3, use list4.extend (list3), later print the ready 4.

4. Swap the first and last item in a list

list5 = [1, 29, 51, 9, 17, 6, 7, 23]
list5[0], list5[-1] = list5[-1], list5[0]
print(list5)
'''
Expected Output:
[23, 29, 51, 9, 17, 6, 7, 1]
'''

Explanation: Given a list list5 have certain values. As mentioned previously, lists are immutable, so any data item can be replaced with another value. Use indexing to replace value. Replace index 0 with the index -1 of list5 and -1o index with 0o index of list5 simultaneously. Now print the updated list list5.

5. Program to subtract a list from another list

a = [1, 2, 3, 5]
b = [1, 2]
l1 = []
for i in a:
    if i not in b:
        l1.append(i)
print(l1)
'''
Expected Output:
[3, 5]
'''

Explanation: Dice 2 lists a and b that have certain values. To subtract the second list from the first list, we can get those items from the list of the first list a that are not present in the second list B. Create an empty list l1. Use by go through the list a, and for each iteration, use and condition together with no to check if I which represents the data item in the list a for that current iteration it is not present in the listing B. And I not present in the list B, to attach I for that iteration to an empty list l1 created at the beginning. In the end, you will have a populated list l1 have those exclusive items to list a.

6. Program to obtain data items from a list that appear an odd number of times

x = [1,2,3,4,5,1,3,3,4]
l1 = []
for i in x:
    if x.count(i) % 2 != 0:
        if i not in l1:
            l1.append(i)
print(l1)
'''
Expected Output:
[2, 3, 5]
'''

Explanation: Given a list X have certain values. Create an empty list l1. Use by go through the list X, and during each iteration, use and condition with .tell() Python Lists function to check the number of occurrences of I a list X where I represents the data item in the list X in each iteration. Use a nested and condition for choosing data items other than the list X having strange occurrences. Now, add I to an empty list l1 created at the beginning. In the end, print the list l1.

Conclution

Therefore, lists can be used in scenarios where you want to store your values ​​temporarily or store them longer. Lists are one of Python's built-in datatypes at the same time as Tuples, dictionaries and sets.

Each of the Python programs mentioned can be performed more efficiently. The modus operandi of troubleshooting these Python programs has been made with the mind of the beginner in mind.. As an example, List Comprehensions can be used whenever feasible to shorten the program. At the same time, you can use user-defined functions to increase the versatility of your code.

Each program is attached with a Expected return what would come if the given inputs are given. Along with the expected output, a Explanation of the program is provided for the beginner to understand in the best possible way.

Apart of this, any beginner can find more list programs on the internet. The Internet abounds in the resources you need to learn anything. Getting perfection on the charts will help one in any domain, either in machine learning or web development in Python.

Even though Lists is a broad subject and needs practice to excel, The above programs will help any beginner to build logic about how to use Loops Y Terms With List functions.

The media shown in this post is not the property of Analytics Vidhya and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.