How to add two equal sized lists into one list with key pairs

Tyler Norman

In Android Studio, I have two lists, specifically:

val distanceResults = Pair(mutableListOf<String>(), floatArrayOf())

It's a pair where each item is its own list; these sublists will always be the same length.

What I need to do is take each index of each list and merge them into one list. So that the end result would look something like:

    list1 -> ("A", "B", "C")
    list2 -> (1F, 2F, 3F)
    finalList -> (
        ["A", 1F],
        ["B", 2F],
        ["C", 3F],
    )

I'm not sure if a filter, flatFiler, map, or something else would do the trick.

Tenfour04

You can zip them, but first you must convert the FloatArray to be iterable so they can be combined. (The standard library doesn't have overloads of all the iterable operators to work with all the different types of arrays.)

val finalList = distanceResults.first zip distanceResults.second.asIterable()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to merge two different sized lists into one?

Adding two values to one key in lists and add the list to dictionary

AutoMapper map one list to two equally sized lists with derived properties

Comparing two dictionaries and checking how many (key, value) pairs are equal

How to compare two lists of dicts for multiple key, value pairs?

How to convert list of lists of key value pairs to dataframe?

How to split a list of lists into dictionary key/values pairs?

How to convert list of lists to dict key value pairs python

How to add key value pairs to dictionary of list when key is missing

split dictionary into two lists , one list is for key, another list is for value

How to map two different lists to one list?

How to merge two lists into one list in python

LINQ How to group by two lists to one list?

How to join two customer lists into one list

How to create two lists/or more, with one list?

How to zip two differently sized lists?

How to zip two different sized lists?

How to Check if elements of two lists are equal to the 3rd list

Turn list of lists into key:value pairs in python

How to check if two key value pairs exists in list of Dictionary in Python

How to add key-value pairs to a list without adding sublists?

Split a list of dictionaries into multiple lists based on uniqueness of one of the key/value pairs in Python

Better to use a list of pairs, or two lists?

Unpacking a list / tuple of pairs into two lists / tuples

Unpacking a list / tuple of pairs into two lists / tuples

Racket: Make list of pairs from two lists

remove pairs of equal elements from two lists in sympy

how to create a list of values from one key-value pair based on the value of another k-v pair from a list of dictionaries with two key-value pairs

How determine two pairs of numbers are equal