-4

Create a list Score that contains the GPA of a student in 3 semesters. Create 3 such students and add them in new list call Students with the name of the student with it as well. Take input from user. for example :

input:

list1 = ['ali','Mudassir','Hasssan']  
list2 = [['3.4','3.5','3.5'],['3.2','3.5','3.6'],['3.4','3.7','3.1']]

output:

[['ali', ['3.3', '3.2', '3.6']], 
 ['Mudassir', ['3.2', '3', '3.6']], 
 ['Hassan', ['3.7', '3.2', '3.9']]]

any suggestion and help will be beneficial for me please give on comment. Thanks in advance

wjandrea
  • 23,210
  • 7
  • 49
  • 68
AncientPro
  • 94
  • 10

4 Answers4

8

No need to use a loop, use zip:

out = list(zip(list1, list2))

Output:

[('ali', ['3.4', '3.5', '3.5']),
 ('Mudassir', ['3.2', '3.5', '3.6']),
 ('Hasssan', ['3.4', '3.7', '3.1'])]

Or for lists:

out = list(map(list, zip(list1, list2))

Output:

[['ali', ['3.4', '3.5', '3.5']],
 ['Mudassir', ['3.2', '3.5', '3.6']],
 ['Hasssan', ['3.4', '3.7', '3.1']]]
Sunderam Dubey
  • 2,294
  • 9
  • 12
  • 22
mozway
  • 81,317
  • 8
  • 19
  • 49
1

This will do:

list1 = ['ali','Mudassir','Hasssan']  
list2 = [['3.4','3.5','3.5'],['3.2','3.5','3.6'],['3.4','3.7','3.1']]

l = []
for i, item in enumerate(list1):
  l.append([item, list2[i]])

print(l)

output:

[['ali', ['3.4', '3.5', '3.5']], ['Mudassir', ['3.2', '3.5', '3.6']], ['Hasssan', ['3.4', '3.7', '3.1']]]
Anurag Dhadse
  • 1,284
  • 12
  • 19
0

Here's a one liner:

l=[[j,list2[i]] for i,j in enumerate(list1)]

print(l)

Explantion:

  • It's a list comprehension.
  • Just creating a new list [j,list2[i]] which first adds first index of list1 and then compiles it with the first index of list2 and then the final value is added to the list l, using a for loop.
Faraaz Kurawle
  • 923
  • 3
  • 22
-2
list1 = ['ali','Mudassir','Hasssan']
list2 = [['3.4','3.5','3.5'],['3.2','3.5','3.6'],['3.4','3.7','3.1']] 

output = [[j, list2[i]] for i, j in enumerate(list1)]

print(output)
    
Seraph
  • 73
  • 8
Sniper1999
  • 65
  • 9