Beta Projects
Beta Projects

Reputation: 446

Make a dictionary in Python from input values

Seems simple, yet elusive, want to build a dict from input of [key,value] pairs separated by a space using just one Python statement. This is what I have so far:

d={}
n = 3
d = [ map(str,raw_input().split()) for x in range(n)]
print d

Input:

A1023 CRT
A1029 Regulator
A1030 Therm

Desired Output:

{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}

Upvotes: 13

Views: 228960

Answers (11)

Emma Lambert
Emma Lambert

Reputation: 1

d = {}
count = 0
data = int(input("How many data do you want to enter?(numbers only): "))
while count < data:
    count = count + 1
    print("Enter a key")
    key = input()
    print("Enter a value")
    value = input()
    d[key] = value
    if count >= data:
        break

print(d)

Upvotes: 0

Dhiren Biren
Dhiren Biren

Reputation: 530

Take input from user:

input = int(input("enter a n value:"))

dict = {}


    name = input() 

    values = int(input()) 

    dict[name] = values
print(dict)

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250941

using str.splitlines() and str.split():

strs="""A1023 CRT
        A1029 Regulator
        A1030 Therm"""
    
dict(x.split() for x in strs.splitlines())

result:

{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}

more info:

str.splitlines([keepends]) -> list of strings

Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

str.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

Upvotes: 5

Pragati Shandilya
Pragati Shandilya

Reputation: 1

I have taken an empty dictionary as f and updated the values in f as name,password or balance are keys.

f=dict()
f.update(name=input(),password=input(),balance=input())
print(f)

Upvotes: 0

n = int(input("enter a n value:"))
d = {}

for i in range(n):
    keys = input() # here i have taken keys as strings
    values = int(input()) # here i have taken values as integers
    d[keys] = values
print(d)

Upvotes: 4

sagar salunkhe
sagar salunkhe

Reputation: 11

record = int(input("Enter the student record need to add :"))

stud_data={}

for i in range(0,record):
    Name = input("Enter the student name :").split()
    Age = input("Enter the {} age :".format(Name))
    Grade = input("Enter the {} grade :".format(Name)).split()
    Nam_key =  Name[0]
    Age_value = Age[0]
    Grade_value = Grade[0]
    stud_data[Nam_key] = {Age_value,Grade_value}

print(stud_data)

Upvotes: 0

rashedcs
rashedcs

Reputation: 3725

n=int(input())
pair = dict()

for i in range(0,n):
        word = input().split()
        key = word[0]
        value = word[1]
        pair[key]=value

print(pair)

Upvotes: 1

Atonu Ghosh
Atonu Ghosh

Reputation: 21

n = int(input())          #n is the number of items you want to enter
d ={}                     
for i in range(n):        
    text = input().split()     #split the input text based on space & store in the list 'text'
    d[text[0]] = text[1]       #assign the 1st item to key and 2nd item to value of the dictionary
print(d)

INPUT:

3

A1023 CRT

A1029 Regulator

A1030 Therm

NOTE: I have added an extra line for each input for getting each input on individual lines on this site. As placing without an extra line creates a single line.

OUTPUT:

{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}

Upvotes: 2

Aakash Balani
Aakash Balani

Reputation: 29

for i in range(n):
    data = input().split(' ')
    d[data[0]] = data[1]
for keys,values in d.items():
    print(keys)
    print(values)

Upvotes: 2

Beta Projects
Beta Projects

Reputation: 446

This is what we ended up using:

n = 3
d = dict(raw_input().split() for _ in range(n))
print d

Input:

A1023 CRT
A1029 Regulator
A1030 Therm

Output:

{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}

Upvotes: 19

piokuc
piokuc

Reputation: 26184

Assuming you have the text in variable s:

dict(map(lambda l: l.split(), s.splitlines()))

Upvotes: 1

Related Questions