How to use a Python dictionary?

前端 未结 7 1041
闹比i
闹比i 2020-12-11 21:24

I am finding it difficult to iterate through a dictionary in python.

I have already finished learning via CodeAcademy and solo learn but still find it tough to go th

相关标签:
7条回答
  • 2020-12-11 21:37

    You need to  check this out

    HEAD FIRST PYTHON

    http://www.headfirstlabs.com/books/hfpython/

    This will change your life and make it interesting to learn PYTHON. You will grasp each concept of Python rather than mugging it up. This book has a very intuitive way of making you understand python.

    This book has a detail explanation on Dictionary and other core concepts.

    0 讨论(0)
  • 2020-12-11 21:51
    # In Python 3.x
    
    hash={'a': 1, 'b': 2}
    
    for k in hash:
        print (str(k) + "," + str(hash[k]))
    
    # OR
    for key, value in hash.items():
        print (str(key) + ',' + str(value))
    
    # OR
    for key, value in {'a': 1, 'b': 2}.items():
        print (str(key) + ',' + str(value))
    
    # OR
    for tup in hash.items():
        print (str(tup[0]) + ',' + str(tup[1]))
    
    0 讨论(0)
  • 2020-12-11 21:54

    If you are using Python 2

    for key, value in d.iteritems():
    

    For Python 3

    for key, value in d.items():
    

    As usual the documentation is the best source for information Python 2 Python 3

    0 讨论(0)
  • 2020-12-11 21:55

    Lots of different documentations and tutorial resources available for Python online, almost each of them are helpful depending upon your need. But most reliable documentation is official documentation of Python website.

    Also please watch youtube videos of the same, many videos of practical implementation of dictionaries and other Python constructs are available in easy to understandable manner.

    Here is sample program for dictionary implementation:

    my_dict = {'name':'Deadpool', 'designation': 'developer'}
    print(my_dict)
    Output: { 'designation': developer, 'name': Deadpool}
    
    # update value
    my_dict['designation'] = 'sr developer'
    
    #Output: {'designation': sr developer, 'name': Deadpool}
    print(my_dict)
    
    # add an item to existing dictionary
    my_dict['address'] = 'New York'  
    print(my_dict)
    # Output: {'address': New York, 'designation': sr developer, 'name': Deadpool}
    
    0 讨论(0)
  • 2020-12-11 21:56
    Dict = { 1 : 'Suri' , 'Fname' : 'Sam' , 'Lname' : 'Saiksas' , 'village' : 'AKP' }
    print ( Dict )
    

    Built-in dict method to create a dictionary:

    Dict2 = dict ( { 1 : 'john' , 2 : 'Pam' , 3 : 'google' } )
    print ( Dict2 )
    

    Using pairs to create a dictionary:

    Dict3 = dict ( [ ('1' , 'Techies') , (2 , 'For') ] )
    print ( Dict3 )
    

    Create a dictionary out of other data structures - list of tuples prints only one value if there are any duplicate tuples in the list:

    list1 = [ (2 , 3) , (4 , 5) , (4 , 5) ]
    print ( dict ( list1 ) )
    

    Assign values to Dictionaries:

    exDict = { }
    exDict [ 'Key_1' ] = 'Welcome'
    exDict [ 'Key_2' ] = 'Good'
    exDict [ 'Key_3' ] = 'Morning'
    exDict [ 'Key_4' ] = 'Deo'
    print ( exDict )
    

    Assign a value set to a key - Assigning multiple values to a single key:

    exDict[ 'Key_1' ] = 'Welcome' , 'Mr' , 'Graham'
    print ( exDict )
    

    Fetching a value from a dictionary using Get Method:

    print ( '\n' , exDict.get ( 'Key_4' ) )
    print ( '\n' )
    
    print ( "|{0}|,|{1}|,{2}|".format ( exDict [ 'Key_1' ] , exDict [ 'Key_2' ] , exDict [ 'Key_3' ] ) )
    

    Deleting items from the dictionary:

    print ( exDict.values ( ) )
    exDict.pop ( 'Key_1' )
    print ( "After popping an element " , exDict )
    print ( '\n' , 'This is the copy of dict' , exDict.copy ( ) )
    print ( '\n' )
    print ( '\n' )
    

    Delete an entire dict:

    print ( exDict.values ( ) )
    print ( '\n' )
    print ( exDict.items ( ) )
    exDict.clear ( )
    print ( '\n' )
    print ( exDict )
    
    0 讨论(0)
  • 2020-12-11 21:58

    Dictionaries are the python equivalent of Hash table. It stores values as key-value pairs. Values can be any python objects whereas, keys need to immutable.

    Below are some of the ways to iterate over a dictionary in python

    # Iterating over the keys.
    for k in dict:
    for k in dict.keys():
    
    #Iterating over the values.
    for v in dict.values():
    
    #Iterating over both the key and values.
    for k,v in dict.items():
    
    0 讨论(0)
提交回复
热议问题