I'm looking for a way to completely delete an element from a tuple based on a condition in Python. The following is a subsection of my code. If it's possible I would like the solution to do its thing in the third if-clause to keep the structure of my main code.
Here is my code:
import ast
data = []
myList=[0,"[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
for a in ast.literal_eval(myList[1]):
Value = a[0]
Type = a[1]
if(Type == "BB"):
Value="XX"
if(Type == "DD"):
Value="YY"
if(Type == "FF"):
Value=""
data.append([myList[0], Value])
print(data)
Current Output:
[[0, 'XX'], [0, 'YY'], [0, ''], [0, 'GG']]
What I need:
[[0, 'XX'], [0, 'YY'], [0, 'GG']]
CodePudding user response:
An easy way to achieve this would be to use the continue
statement in your third conditional. Continue statements can be used in loops to skip the execution of the remaining code in the loop and return control to the beginning of the loop at the next iteration.
In your case:
if(Type == "FF"):
continue
data.append([myList[0], Value])
would mean that if type is 'FF' then the data.append statement is skipped and the entry is not added to your list.
CodePudding user response:
You just need to check if Value
variable is holding anything or not. If it does then append the item in list otherwise don't.
So as you asked for a solution to be implemented in another if condition, you can do as follows:
if Value:
data.append([myList[0], Value])
Here, blank string ""
is considered as False
, so the item won't be appended if Value is a blank string.
CodePudding user response:
You could "filter" out data that you append within the last value. i.e. you prevent appending that data entry to the list if it doesn't meet the requirements you set. There are a few ways you could do this
- You could set it to only append values that match the condition
import ast
data = []
myList=[0,"[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
for a in ast.literal_eval(myList[1]):
Value = a[0]
Type = a[1]
if(Type == "BB"):
Value="XX"
if(Type == "DD"):
Value="YY"
if(Type != "FF"):
data.append([myList[0], Value])
print(data)
- Alternatively you could use
continue
to skip the last part of the loop. This one looks nicer and keeps your code consistent.continue
tells python to "skip everything until the next loop".
import ast
data = []
myList=[0,"[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
for a in ast.literal_eval(myList[1]):
Value = a[0]
Type = a[1]
if(Type == "BB"):
Value="XX"
if(Type == "DD"):
Value="YY"
if(Type == "FF"):
continue
data.append([myList[0], Value])
print(data)
Both should give you the result you are looking for
CodePudding user response:
import ast
data = []
myList=[0,"[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
for a in ast.literal_eval(myList[1]):
Value = a[0]
Type = a[1]
if(Type == "BB"):
Value="XX"
if(Type == "DD"):
Value="YY"
if(Type == "FF"):
Value=""
if not Value == "":
data.append([myList[0], Value])
print(data)
# [[0, 'XX'], [0, 'YY'], [0, 'GG']]
You can also make your code shorter, more clear and more Pythonic like so:
import ast
data = []
my_lst = [0, "[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
type_to_val = {'BB': 'XX', 'DD': 'YY', 'FF': ''}
for tup in ast.literal_eval(my_lst[1]):
(cur_val, cur_type) = tup
try:
cur_val = type_to_val[cur_type]
except KeyError:
pass
if cur_val != "":
data.append([my_lst[0], cur_val])
print(data)
CodePudding user response:
You can do everything needed it a list comprehension:
import ast
myList=[0,"[('AA', 'BB'), ('CC', 'DD'), ('EE', 'FF'), ('GG', 'HH')]"]
data = [(myList[0], value) for value, type_ in ast.literal_eval(myList[1] if type_ != "FF"]
print(data)