3

I want to write this below code in one line. I have lots of data. so the page goes on. So i want to shrink it. How to make it possible. I know it is possible in python. Help me with some solutions.

data['url']=url
data['user agent']=userAgent
data['browser']=browser
data['uniqueId']=uniqueId
data['ip']=ip
data['language']=language
and its going on.

I tried this but it fails.

data['url','user agent','browser'...] = url,useragent,browser....
luis.parravicini
  • 1,214
  • 11
  • 19

4 Answers4

5
keys = ("url", "ip", "language")
values = ("http://example.com", "93.184.216.34", "en")

# if you want to update an existing dict:
data = {}
data.update(zip(keys, values))

# if you just want to create a dict:
data = dict(zip(keys, values))
Amadan
  • 191,408
  • 23
  • 240
  • 301
2

If you want to set all the values at once, you could do something like this:

data = { 'url': url, 'user agent': userAgent, ... }

If data already has... data, you could update it with:

data.update({ 'url': url, 'user agent': userAgent, ... })
luis.parravicini
  • 1,214
  • 11
  • 19
0

You can use dict comprehension:

keys = ['url','user agent','browser']
vals = [url,useragent,browser]
data = {key:val for key,val in zip(keys,vals)}
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
0

You could do a for loop:

Example:

for key, value in Data:
    finalData[key] = value
Community
  • 1
  • 1