-1

I have two lists (both are already ordered correctly and contain 180.000 items each). The first one contains the filenames.

filenames[0]
s01_l01/1000_1.png

The second list contains the labels of the files.

labels[0]
'1BK 7475'

How do I now get a list, which looks like this: [[filename_1, label_1], [filename_2, label_2],..., [filename_n, label_n]]?

list_of_filenames_labels[0]
[s01_l01/1000_1.png, '1BK 7475']

Thanks a lot!

Tobitor
  • 1,388
  • 1
  • 23
  • 58
  • 3
    Does this answer your question? [How to merge lists into a list of tuples?](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples) – mkrieger1 May 26 '20 at 13:22

4 Answers4

1

You can do that with a simple map with lambda function

a = ['1','2','3']
b = ['a','b','c']
c = list(map(lambda x, y: [x,y], a, b))

The output for c would be

c = [['1', 'a'], ['2', 'b'], ['3', 'c']]
Youstanzr
  • 605
  • 1
  • 8
  • 16
0

The output you specified is similar to the output of zip, except that it is a list of lists rather than a list of tuples. This would convert them to lists:

[list(t) for t in zip(filenames, labels)]
alani
  • 12,573
  • 2
  • 13
  • 23
0

Here:

list_of_filenames_labels = [[filename,label] for filename,label in zip(filenames,labels)]
Red
  • 26,798
  • 7
  • 36
  • 58
-1

if both list have same length, you can use enumerate function

list_of_filenames_labels = [[filename, labels[i]] for i, filename in enumerate(filenames)]

Piyush
  • 91
  • 5