List comprehension

Creating lists in Python is very easy. One can use [0] * 50 to create a list of 50 elements filled with 0s. Another way that provides finer-grained control over the elements is iterating with a for loop and appending elements one by one. Yet, there is another way of creating a list through list comprehension. In case we would like to create a list of squares for numbers 1...100, we could do that with:
squares = []
for n in range(1, 100):
	squares.append(n * n)
print(squares)
squares = [n * n for n in range(1, 100)]
print(squares)
Both of these programs would print the same output and create the same list of squares - [1, 4, 9, 16, 25, 36, 49, 64, 81, ..., 9216, 9409, 9604, 9801].
So, the syntax for list comprehension is:
  1. The comprehension fits inside opening and closing brackets []
  1. The for loop iterates over values
  1. The elements of the list come before the for keyword and can be dependent on the variable of the for loop.
 
In case of sending birthday invitations, imagine you have created a list of names that we would like to invite, but we have accidentally made them all lowercase. Now we can use a list comprehension to fix that:
invites = [
	'anna', 
	'alice', 
	'bob', 
	'simon', 
	'thomas'
]
fixed = []
for name in invites:
	fixed.append(name.title())
print(fixed)
invites = [
	'anna', 
	'alice', 
	'bob', 
	'simon', 
	'thomas'
]
fixed = [name.title() for name in invites]
print(fixed)
Both of these programs will print ['Anna', 'Alice', 'Bob', 'Simon', 'Thomas'].

Challenge

Given n names and surnames, you are asked to extract the names and print both the initial list and the list with only names.
The first line of the input contains a single integer n. The next n lines contain name surname pairs.
The program should print the initial list first and then the list with only names.
Input
Output
3 Marshall Mathers Peter Hernandez Curtis Jackson
Marshall Mathers Peter Hernandez Curtis Jackson Marshall Peter Curtis
Tip
You can use .split()[0] to get the first element after splitting the string by whitespace.
 
You can even print with list comprehension:
l = ['hi', 'my', 'name', 'is']
[print(item) for item in l]
hi
my
name
is
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue