Skip to content

Instantly share code, notes, and snippets.

@khalidelboray
Created August 10, 2020 12:13
Show Gist options
  • Select an option

  • Save khalidelboray/c60e6050374cfe519f028d2a102f0319 to your computer and use it in GitHub Desktop.

Select an option

Save khalidelboray/c60e6050374cfe519f028d2a102f0319 to your computer and use it in GitHub Desktop.
hints
file_lines = ['a line','another one ','',' ','last','> we want this','> and this']
print("First Code said \n")
# Get lines that starts with ">" and not empty
for line in (l for l in file_lines if l.strip() and l.startswith(">") ) :
print(line)
# An empty line dosen't start with a ">" so the code can be shortened to
print("Second code said \n")
for line in (l for l in file_lines if l.startswith(">") ) :
print(line)
my_dict = {'a':[1,2,3]}
print(my_dict)
# Now we append a value to the 'b' key in my_dict, using setdefault before append setes the value of 'b' to an empty array if it's not already set
my_dict.setdefault('b',[]).append(3)
print(my_dict)
if 'c' not in my_dict:
my_dict['c'] = []
my_dict['c'].append(4)
print(my_dict)
First Code said
> we want this
> and thisSecond code said
> we want this
> and this
{'a': [1, 2, 3]}
{'a': [1, 2, 3], 'b': [3]}
{'a': [1, 2, 3], 'b': [3], 'c': [4]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment