convert list = {"x-y", "w-z", "a-b"} into dict={x:y. w:z,a:b}

convert list to array java 8
convert list to int array java
convert list to array c#
convert list to array python
convert list to arraylist java
convert column to comma separated list
convert list of list to array java
write a program to convert list to array

I have searched a lot on how to convert list into dictionary and found solutions but none of them have given the solution.

I have a list

LIST = {"x-y", "w-z","a-b"}

Now i want to convert it into a dictionary which should look like

DICT = {'x':'y', 'w':'z', 'a':'b'}

When faced with such issues it's always good to think a few things through.

What is a dictionary?

A dictionary is conceptually a key-value storage meaning each element in a dictionary is a pair of (key, value). We can assign values to a dictionary using d[key] = value and we can retrieve a value using d[key].

Now we know how we can put values into a dictionary.

What's the format of my input data? and What do I need to do to convert my input data?

Or input format is <key>-<value> which means we can use split to extract the key and value.

>>> "x-y".split("-")
['x', 'y']

This allows us to do this:

DICT = {}
for element in LIST:
    parts = element.split("-")

    if len(parts) != 2:
          # handle this case? (see below)

    (key, value) = (parts[0], parts[1])
    DICT[key] = value

What do I do on errors?

Can my input format contain x-y-a. What is this? Is this (x-y,a) or is it (x,y-a)? Or is it illegal? How do I deal with -? How do I deal if there's no - in it? I.e.

>>> dict(x.split('-', 1) for x in ["a"])

Would throw an exception that you might want to handle if you use this approach:

>>> dict(x.split('-', 1) for x in ["a"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 1; 2 is required

dict(x.split('-', 1) for x in only works for correctly formated inputs for example.

Are there other ways to construct a dictionary?

Reading through the documentation you'll find that there's dict that takes a list of 2-tuples. Which means you can do this:

>>> dict([('a','b'),('c','d')])
{'a': 'b', 'c': 'd'}

Convert List to Array in Java, In this post, we will see how to convert list to array in Java. 1. List.toArray() 2. Java 8 Streams API 3. Guava's FluentIterable & Iterable class Python | Convert List of lists to list of Strings Interconversion of data is very popular nowdays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings.

input = {"x-y", "w-z","a-b"} 
output = dict(x.split('-', 1) for x in input)

Convert Column to Comma Separated List Online, Use this tool to convert a column into a Comma Separated List. Copy your column of text in Excel; Paste the column here (into the leftmost textbox); Copy your  Get a comma-separated string from a list of numbers. If you simply wish to convert a comma-separated string, then try the below shortcut: numList = [20, 213, 4587, 7869] print(str(numList).strip('[]')) The output is as follows: 20, 213, 4587, 7869. Alternatively, we can use map() function to convert the items in the list to a string.

d = {x.split('-')[0]: x.split('-')[1] for x in LIST}

Free Online Comma Separator Tool, Do you often need to take a spreadsheet of data and convert to a comma-​delimited list? Be it for taking a list of zip codes or names to make an SQL query, or to  You may then use this template to convert your list to pandas DataFrame: from pandas import DataFrame your_list = ['item1', 'item2', 'item3',] df = DataFrame (your_list,columns=['Column_Name']) In the next section, I’ll review few examples to show you how to perform the conversion in practice.

Using dict


l = {"x-y", "w-z","a-b"}
print( dict([i.split("-") for i in l]) )


{'a': 'b', 'x': 'y', 'w': 'z'}

Note: l is a set not a list

Convert List to Array in Java, Converting between List and Array is a very common operation in Java.The best and easiest way to convert a List into an Array in Java is to use  Given a list, write a Python program to convert the given list to string. There are various situation we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.

d = {}                //simple dictionary declaration
for i in LIST:        // for loop to iterate and access each element of LIST
    d[i[0]] = i[2]    // this will use 1st letter of each word as key and 3rd letter as value of dictionary

Python, Using sorted() function will convert the set into list in a defined order. The only drawback of this method is that the elements of the set need to be sortable. This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma (, ). This approach is a bit faster but suffers from readability. filter_none

Python, Python | Convert list to Python array. Sometimes while working in Python we can have a problem in which we need to restrict the data elements to be just of one  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Python tips, There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display. First, if it is a list of strings, you may simply use join  l <- list(a = list(var.1 = 1, var.2 = 2, var.3 = 3) , b = list(var.1 = 4, var.2 = 5, var.3 = 6) , c = list(var.1 = 7, var.2 = 8, var.3 = 9) , d = list(var.1 = 10, var.2 = 11, var.3 = 12) ) has now a length of 4 and each list in l contains another list of the length 3.

Convert List to Array in Java, Converting between List and Array is a very common operation in Java. The best and easiest way to convert a List into an Array in Java is to  ''' Convert list of string to a string with whitespace as seperator ''' # Join all the strings in list fullStr = ' '.join (listOfwords) Convert list of string to a string with whitespace as seperator. # Join all the strings in list. fullStr = ' '.join(listOfwords) It returned followings string i.e. This is a sample program.

  • your LIST object is not a list, but a set
  • i variable will iterate through values of LIST like "x-y". Now i[0] i.e. x, which will become key and i[2] i.e. y will be its value.Similar for all values in LIST and after loop, the value of d will be {"x":"y","a":"b","w":"z"}
  • Please edit your answer and explain your code there. Code in comments is hard to read.