0

I know i can convert list of tuples to dict like that

list_of_tuples = [("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)]
new_dict = {}
new=dict{list_of_tuples}
print(new)
#{'k': 167, 'z': 179, 'u': 179, 'm': 267, 'a': 445, 'l': 134}

But what to do when i got user INPUTS this [("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)] as string

What User unputs in line as string

([("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)])

i tried with Json to convert to dict but couldn't because its list of tuples

WinterAK
  • 33
  • 5
  • From a user input you can only get a string never a list of tuples. – hpchavaz Jan 22 '22 at 14:54
  • 1
    You might want to clarify if you receive a single string like `'[("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)]'` or what you have in the question. – Sash Sinha Jan 22 '22 at 14:54
  • yes user input this one [("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)] as string – WinterAK Jan 22 '22 at 14:55

2 Answers2

0

First, convert the string to a list and then you can do dictionary comprehension like this:

import ast
my_list = ast.literal_eval("[("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)]")

d = {k:v for k,v in my_list}
Bes Dollma
  • 383
  • 3
  • 10
  • 2
    Here ure giving list in code itself you need to get list form user as input – WinterAK Jan 22 '22 at 14:54
  • When I posted the answer this was not clear from the question. However, to convert string to list look here: https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list – Bes Dollma Jan 22 '22 at 14:59
  • thanks i need to read ast module – WinterAK Jan 22 '22 at 15:04
  • Do i understand it right with ast module it can convert inputted Lists sets tuples to lists sets tuples if they have right brackets in input? – WinterAK Jan 22 '22 at 15:09
0

Use a regular expression to replace (, , inside tuple, and ) in the string with {, : and } respectively.

>>> i = '[("k", 167), ("z", 179), ("u", 179), ("m", 267), ("a", 445), ("l", 134)]'
>>> j = re.sub(r'\(("\w"), (d+)\)', r'{\1:\2}', i)
>>> d = json.loads(j)
>>> d
[{'k': 167}, {'z': 179}, {'u': 179}, {'m': 267}, {'a': 445}, {'l': 134}]

Explanation of r'\(("\w"), (d+)\)', r'{\1:\2}':

  • \( and \) mean "read opening and parenthesis instead of grouping"
  • the regular ( and ) is a grouping selection which can be used in the replacement with \n, where n is the position of the group. This is used in the regular expression above twice, so we don't lose the text within double quotes and the number within each tuple
  • \w corresponds to a sequence of characters that represent a word
  • \d+ corresponds to one or more consecutive digits

https://docs.python.org/3/library/re.html

  • actually i tried smt like that its good way too can u explain 're.sub(r'\(("\w"), (d+)\)', r'{\1:\2}', i)' if u busy nvm – WinterAK Jan 22 '22 at 16:02