How can I define a function that takes a list of first names and a list of last names, and returns a list of the corresponding full names where the last name is initialized

0 votes
I need to define a function called initializeNames that takes a list of first names and a list of last names, and returns a list of the corresponding full names where the last name is initialized. Assume that the input lists lengths are equal.

Example:

Input -

firstNames = ['Albert', 'Isaac']

lastNames = ['Einstein', 'Newton']

Output - ['Albert E.', 'Isaac N.']
Apr 21 in Python by subi
• 120 points
133 views

2 answers to this question.

0 votes

Hi@subl,

There is no pre-built function available to do your task I think. So, you have to create your own customize code according to your need. You can see this below code. It is created according to your example.

firstNames = ['Albert', 'Isaac']
lastNames = ['Einstein', 'Newton']
for i in firstNames:
    for j in lastNames:
        c.append(i+' '+j[0]+'.')    
d=[c[0],c[3]]

Hope this will give you some idea.

answered Apr 21 by MD
• 55,020 points
0 votes

Hey, @Subi,

Regarding your query, you can go through this process also, the code shown below:

str1= "albert einstein"
str2= "issac newton"

list1 =str1.split()[0].capitalize()
list2 =str1.split()[1].capitalize()

list3 =str2.split()[0].capitalize()
list4 =str2.split()[1].capitalize()

result1 =[]
result2 =[]

result1.append(list1)
result1.append(list2[0])
result2.append(list3)
result2.append(list4[0])

temp =" "
out=temp.join(result1)
put=temp.join(result2)
print(out, put)

I know its a bit lengthy process, but you will understand better. Capitalize function in python will always convert the first letter of any string into the upper-case.

 I hope this will help.

answered Apr 21 by Gitika
• 36,850 points

Related Questions In Python

0 votes
1 answer

How do I get the path and name of the file that is currently executing?

Hello @kartik, Since Python 3 is fairly mainstream, ...READ MORE

answered May 11 in Python by Niroj
• 57,260 points
89 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,780 points
263 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,780 points
243 views
+5 votes
7 answers

Docker swarm vs kubernetes

Swarm is easy handling while kn8 is ...READ MORE

answered Aug 27, 2018 in Docker by Mahesh Ajmeria
1,316 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
672 views
0 votes
1 answer