473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to access only the key at the 0th or 1st position of the dictionary in python

Eg. Python{ 'Good' : '0', 'Bad' :'9', 'Lazy' : '7'}
how do i access and print only 'Good' or 'Bad' or 'Lazy'
Sep 15 '10 #1
3 1768
bvdet
2,851 Expert Mod 2GB
Dictionary method keys().

Expand|Select|Wrap|Line Numbers
  1. >>> dd = { 'Good' : '0', 'Bad' :'9', 'Lazy' : '7'}
  2. >>> dd.keys()
  3. ['Bad', 'Good', 'Lazy']
  4. >>> dd.keys()[0]
  5. 'Bad'
  6. >>> 
Sep 15 '10 #2
Hey thanks for the reply...
But now I need to access the key names dynamically in a program. Eg.

Expand|Select|Wrap|Line Numbers
  1. a= raw_input (" which is the final attribute:")
  2. for i in python.items():
  3.     if python.items()[i] == a:
  4.         finalAttribute = python.items()[i]
This giving me error saying
Traceback (most recent call last):
File "C:/Python27/test1.py", line 11, in <module>
if somedict.items()[i] == a:
TypeError: list indices must be integers, not tuple
Sep 15 '10 #3
bvdet
2,851 Expert Mod 2GB
Dictionary method items() returns a list of tuples.
Sep 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Samuel Schulenburg | last post by:
Who should I contact about accessing my account on Starship. I need to obtain a new password and access process. I think i am having a memory lapse as it has been over a year sense I have had a...
9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
7
by: Nainto | last post by:
I have been searching and searching and cannot find a way to click at a certain position in python. (Similar to http://tinyurl.com/9tap2 in Cocoa) Does anyone know of a way? Thanks.
0
by: viv.mee | last post by:
Hi, Could anybody tell me how to connect to a microsoft Access database using Python? Quick sample of code would help .. much appreciated..Thanks viv
0
by: Samuel | last post by:
Introduction ------------ Spiff Guard is a library for implementing access lists in Python. It provides a clean and simple API and was implemented with performance and security in mind. It was...
5
by: Sardonic | last post by:
all help is appreciated: >>> x = urllib.urlopen("http://en.wikipedia.org/wiki/Python") >>> x.read() (returns error page)
5
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}}...
4
by: psbasha | last post by:
Hi , The below snippet of the code is returing the empty dict,but I would like to get the values in the dict as {1:200,2:300,3:400}. Could anybody suggest what method I have to use to return...
6
by: gita ziabari | last post by:
Hello All, The following code does not work for unicode characters: keyword = dict() kw = 'ÇÅÎÓËÉÈ' keyword.setdefault(key, ).append (kw) It works fine for inserting ASCII character. Any...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.