0

I might be over complicating this but it's stumped me. I have two lists: 1. names 2. weights

Joe weighs 100. John weighs 200, etc. I want to create a new list which would look like so when printed out:

...names_weights 
[Joe, 100],
[John, 200], 
[Ted,150],
[Bill,200]...

Basically, each person and their corresponding weight are an element in the new list. However, when I try to do this I get a list where all the names are one element, and all the weights are an element. So I get a two element list instead of a 4 element list.

Is there something I'm missing when creating the new list?

2 Answers2

0

You can use the zip function in Python:

zip(names, weight)
double-beep
  • 5,031
  • 17
  • 33
  • 41
ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

Here is a way to do it using list comprehensions, though it's probably not the best way:

l = [['Joe', 100],
     ['John', 200], 
     ['Ted',150],
     ['Bill',200]]
names, weights = [name for [name,weight] in l],[weight for [name,weight] in l]

l2 = [names,weights]

print(l2)

Output:

[['Joe', 'John', 'Ted', 'Bill'], [100, 200, 150, 200]]
Red
  • 26,798
  • 7
  • 36
  • 58