Deletion of a Tuple

Deletion of a Tuple

Lesson Details:
June 29, 2020


I: Introduction

Hm... it's hard to start when I'm so tired. But I will try...

What is a programming language? It's a set of rules for writing programs. A computer can only understand the language you want to use, not plain English or Russian or any other human language. And since computers are actually very dumb, they can only understand a limited set of instructions called a programming language.

Programming languages are designed to be easy for people to read and write, but difficult for a computer to execute. This ensures that humans have a good idea what a program does before it is run, but the computer can execute it efficiently. It's important to make sure that a program does the right thing without having to look at it too hard.

In this article I will talk about Python, one of the most popular programming languages, and some of the common ways in which programs get written.

II: Body

Deletion of a tuple

Deletion of a tuple is a process that removes a tuple from a list. My first deletion of a tuple was a failure, because I did not know how to do it properly. The code was not executed properly and caused some problems. After some time I learnt more about tuples and deletion of tuples and I developed some techniques for deleting them from lists. In this section I will tell you how I failed and how I succeeded in my first attempt to delete a tuple from a list.

The code is as follows:

my_list = [1,2,3] my_tuple = ('a', 'b') del my_tuple my_list print(my_list)

Output: ['a', 2, 3]





Here's another example:

my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple my_list print(my_list)

Output: ['qwerty', 'asdfghx', 'zxcvbn']





As you can see, two tuples are deleted from the list. The first one is deleted correctly, but the second one is not deleted correctly. When the second tuple is deleted, the list loses one index but still has four elements. It's because tuples are not numbers! If you delete the tuple from the list it changes the number of elements in your list to three, not four. You can use len() function to check how many elements are left in your list after you delete the tuple. So if you delete one tuple with index 1, you need to replace it with an element with index 2 + 1 = 3, which is the next index after the deleted element. The following example will show how the program works:

my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple my_list print(my_list) my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple print(len(my_list)) print(my_list) my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple my_list = ['qwerty', 'asdfgh', 'zxcvbn'] print(my_list) print(len(my_list)) print('It does not work!') print('Why?')

Output: ['qwerty', 'asdfgh', 'zxcvbn'] 2 ['qwerty', 'asdfgh'] 1 ['qwerty', 'asdfgh', 'zxcvbn'] It does not work! Why?





As you see, after deleting one element from the list, there are now three elements in it instead of four. In order to fix this problem we need to replace the deleted tuple with another element from the list, so now we have three elements instead of two. Unfortunately, Python is not able to do this by itself so we have to do this manually. To fix this problem we need to use append() function, as shown below:

my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple my_list print(my_list) my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple print(len(my_list)) print(my_list) my_list = ['qwerty', 'asdfgh', 'zxcvbn'] my_tuple = ('x', 'y') del my_tuple my_list = ['qwerty', 'asdfgh', 'zxcvbn'] print(my_list) print('It does not work!') print('Why?')

Output: ['qwerty', 'asdfgh', 'zxcvbn'] 2 ['qwerty', 'asdfgh'] 1 ['qwerty`, asdfgh, zxcvbn] It does not work! Why?



loader
Course content