0
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', '45'), ('',''),()] remove empty tuple without filer()
Use if
12 Réponses
+ 2
Jayakrishna🇮🇳 it is a list of tuples
+ 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.
+ 1
Avinesh
if not tuple
should do it too.
+ 1
Oh. Yes. Thanks for clarification..
Oma Falk then it works as @Avinesh said..
+ 1
Jayakrishna🇮🇳 it almost works everything as Avinesh says😉
+ 1
U can use a list comprehension:
tuples = [t for t in tuples if t]
https://code.sololearn.com/czCzbLpH9uuj/?ref=app
+ 1
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', '45'), ('',''),()]
for x in tuples:
if len(x) == 0:
tuples.remove(x)
0
This should work I guess.
for tuple in tuples:
if tuple == ():
tuples.remove(tuple)
0
Tuple are immutable.
That is list. But tagged tuple.
Is it list or tuple?
0
Avinesh yes. I edited, I seen only () so assumed as tuple as it was named also.. Tagged tuple too..
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)
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)