tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', '45'), ('',''),()] remove empty tuple without filer() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', '45'), ('',''),()] remove empty tuple without filer()

Use if

24th Aug 2020, 1:01 PM
Mounika
Mounika - avatar
12 Answers
+ 2
Jayakrishna🇮🇳 it is a list of tuples
24th Aug 2020, 1:39 PM
Oma Falk
Oma Falk - avatar
+ 2
Oma Falk sure but it wasn't there in the question so I did not include any other case. Jayakrishna🇮🇳 yes probably you misinterpreted the question at first.
24th Aug 2020, 1:47 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh if not tuple should do it too.
24th Aug 2020, 1:40 PM
Oma Falk
Oma Falk - avatar
+ 1
Oh. Yes. Thanks for clarification.. Oma Falk then it works as @Avinesh said..
24th Aug 2020, 1:45 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 it almost works everything as Avinesh says😉
24th Aug 2020, 1:49 PM
Oma Falk
Oma Falk - avatar
+ 1
U can use a list comprehension: tuples = [t for t in tuples if t] https://code.sololearn.com/czCzbLpH9uuj/?ref=app
25th Aug 2020, 6:03 PM
portpass
+ 1
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', '45'), ('',''),()] for x in tuples: if len(x) == 0: tuples.remove(x)
25th Aug 2020, 6:23 PM
Subhash
Subhash - avatar
0
This should work I guess. for tuple in tuples: if tuple == (): tuples.remove(tuple)
24th Aug 2020, 1:22 PM
Avinesh
Avinesh - avatar
0
Tuple are immutable. That is list. But tagged tuple. Is it list or tuple?
24th Aug 2020, 1:38 PM
Jayakrishna 🇮🇳
0
Avinesh yes. I edited, I seen only () so assumed as tuple as it was named also.. Tagged tuple too..
24th Aug 2020, 1:49 PM
Jayakrishna 🇮🇳
0
I just wondered Mounika if the ('','') is also for you an empty tuple. If yes then the above Avinesh code can be extended as follows: for tuple in tuples: if(tuple == ()): tuples.remove(tuple) for tuple in tuples: flag = False for item in tuple: if(item != ''): flag = False break else: flag = True if(flag): tuples.remove(tuple) print(tuples)
24th Aug 2020, 2:41 PM
JaScript
JaScript - avatar
0
Avinesh Because the list size changes the loop would only iterate through every second item in the list. But this should work: for i in range(len(tuples) - 1, -1, -1): if not tuples[i]: tuples.pop(i)
24th Aug 2020, 3:34 PM
Seb TheS
Seb TheS - avatar