As you've already understood I'm a beginner and am trying to understand what the "Pythonic way" of writing this function is built on. I know that other threads might include a partial answer to this, but I don't know what to look for since I don't understand what is happening here.
This line is a code that my friend sent me, to improve my code which is:
import numpy as np
#load_data:
def load_data():
data_one = np.load ('/Users/usr/... file_name.npy')
list_of_tuples = []
for x, y, label in data_one:
list_of_tuples.append( (x,y) )
return list_of_tuples
print load_data()
The "improved" version:
import numpy as np
#load_data:
def load_data():
data_one = np.load ('/Users/usr.... file_name.npy')
list_of_tuples = [(x,y) for x, y, label in data_one]
return list_of_tuples
print load_data()
I wonder:
- What is happening here?
- Is it a better or worse way? since it is "Pythonic" I assume it wouldn't work with other languages and so perhaps it's better to get used to the more general way?