I'm trying to append to a list in Python 3 but want to do this in the shortest amount of lines possible. In order to do this, I have created a blank 1D array and used basic iteration to append to it. I want to append the 2d array within the iteration as well as allowing the user to be able to input their data to the 2d arrays. I know the code below doesn't work. However, I wanted to know if this is possible to achieve?
I'd like the output to be like the following:
[First name, Second name], [First name, Second name], [First name, Second name]
Array = []
for i in range (0,4):
Array.append([input("First name")][input("Second name")])
print(Array)
Your code seems wrong, it's unclear what you want to do. I'm assuming you want a list like :
[ ["Mark", "Wahlberg"], ["Matt", "Damon"] ]
you can do that like :
Enter your name like : "Matt Damon", it splits it into 2 strings and creates a list.
Your question is a bit unclear. I understand that you want the output as:
And that you want least number of lines as possible, so this is how short I was able to make it using List Comprehension:
Or make it one line with :