Warning: session_start(): open(/tmp/sess_1ca973f42ac2ddb912eb528b575125f3, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Append Value To A List If Not Already Present Using Python - LearnShareIT

How To Append Value To A List If Not Already Present Using Python

In this article, we will show you how to append value to a list if not already present using Python. It is used to prevent data redundancy so the data will not have any duplicate values. To do that, you can check whether the value exists before adding it to a list or the set method in Python. Let’s learn more about it with the explanation and example below.

Append Value To A List If Not Already Present Using Python

To append value to a list if not already present using Python, you can check if this value exists in a list or not before adding it to the list. Another solution, you can use the set data structure, it is unordered and does not allow duplicate values. So, the duplicate values will not be added to the set data structure.

Add a value to a list 

You can add a value to a list if it does not already exist by checking if this value exists or not before adding it to the list.

Look at the example below.

# Add a value to a list if not already present
def addValue(element, currentList = list()):
    # Check if this value exists in the list or not before adding
    if element not in currentList:
        currentList.append(element)
        print('Successful!')
    else:
        print('The value is duplicated!')
    return currentList

# Create a new list
numList = [5, 7, 9, 3, 2, 11, 6, 8, 10]

# Try this function
element = 1000
numList = addValue(element, numList)
print(numList)

# Another value
element = 10
numList = addValue(element, numList)
print(numList)

Output

Successful!
[5, 7, 9, 3, 2, 11, 6, 8, 10, 1000]
The value is duplicated!
[5, 7, 9, 3, 2, 11, 6, 8, 10, 1000]

Add a list to a list 

You can add the values of a list to a list if the value does not already exist by checking if this value exists or not before adding it to the list.

Look at the example below.

# Create a new list
numList = [5, 7, 9, 3, 2, 11, 6, 8, 10]

# A list to be added
values = [1, 5, 7, 22, 8, 66]

# Add this list to the current list
for element in values:
    if element not in numList:
        numList.append(element)

print('The new list: ' + str(numList))

Output

The new list: [5, 7, 9, 3, 2, 11, 6, 8, 10, 1, 22, 66]

Use the set data structure

You can add values to a list if they do not already exist with the set data structure. The set data structure is unordered and does allow duplicate values. So the performance of the code will be higher if you use the set data structure.

Look at the example below.

# Create a new list
numList = [5, 7, 9, 3, 2, 11, 6, 8, 10]

# A list to be added
values = [1, 5, 7, 22, 8, 66]

# Convert the old list to a set
numList = set(numList)

# Add the 'values' list to the old list without duplicates
numList = list(numList.union(values))
print('The new list: ' + str(numList))

Output

The new list: [1, 2, 3, 66, 5, 6, 7, 8, 9, 10, 11, 22]

Note that if you perform this solution, the order of elements in the old list will also change from the original.

Summary

We have shown you how to append value to a list if not already present using Python. To do that, you can check whether the value exists before adding it to the list or the set data structure. If you only care about the performance of the code without the order of elements, you should use the set data structure. Otherwise, you should check if the value exists or not before adding it to the list.

Leave a Reply

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