0% found this document useful (0 votes)
5 views4 pages

Python API: Dictionary & List Methods

This document provides a comprehensive guide on Python dictionary and list methods essential for handling API data in JSON format. It details various methods for dictionaries and lists, including examples for better understanding, and emphasizes the importance of mastering these methods for effective data manipulation. The guide concludes by highlighting how these methods facilitate the fetching and processing of API responses.

Uploaded by

himanshuso388
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python API: Dictionary & List Methods

This document provides a comprehensive guide on Python dictionary and list methods essential for handling API data in JSON format. It details various methods for dictionaries and lists, including examples for better understanding, and emphasizes the importance of mastering these methods for effective data manipulation. The guide concludes by highlighting how these methods facilitate the fetching and processing of API responses.

Uploaded by

himanshuso388
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Comprehensive Guide: Python Dictionary and List Methods for API Data

Handling
When working with APIs, the data we receive is usually in JSON format. In Python, JSON is
converted into dictionaries (key-value pairs) and lists (ordered collections). To extract and
manipulate this data effectively, it is important to understand the available methods. This
document covers dictionary and list methods in detail, with examples and explanations in
simple language.

1. Dictionary Methods
A Python dictionary is an unordered collection of key-value pairs. Keys are unique
identifiers, and values are the associated data.
Example: {'word': 'example', 'partOfSpeech': 'noun'}

keys()
Returns all the keys of the dictionary.
Example: [Link]() → ['word', 'meanings']

values()
Returns all the values of the dictionary.
Example: [Link]() → ['example', [...]]

items()
Returns key-value pairs as tuples.
Example: [Link]() → [('word', 'example'), ('meanings', [...])]

get(key, default)
Fetches the value for a key. Returns 'default' if key not found.
Example: [Link]('word', 'Not Found')

[] operator
Directly access a value using its key.
Example: data['word'] → 'example'

update()
Adds or updates key-value pairs.
Example: [Link]({'origin': 'Latin'})

pop(key)
Removes the specified key and returns its value.
Example: [Link]('word') → 'example'
popitem()
Removes and returns the last inserted key-value pair.
Example: [Link]()

clear()
Removes all items from the dictionary.
Example: [Link]() → {}

copy()
Creates a shallow copy of the dictionary.
Example: new_data = [Link]()

fromkeys(iterable, value)
Creates a new dictionary from a list of keys, all with the same value.
Example: [Link](['a','b'], 0) → {'a':0, 'b':0}

setdefault(key, default)
Returns the value of a key, and inserts key with default value if not found.
Example: [Link]('origin','unknown')

in keyword
Checks if a key exists in dictionary.
Example: 'word' in data → True

2. List Methods
A Python list is an ordered collection of items, enclosed in square brackets []. Lists are
commonly used in API responses to store multiple objects or entries.
Example: [1, 2, 3, 'apple']

append(x)
Adds an item to the end of the list.
Example: [Link]('banana')

insert(i, x)
Inserts an item at index i.
Example: [Link](1, 'orange')

extend(iterable)
Adds multiple items from another list.
Example: [Link](['grape', 'mango'])

remove(x)
Removes the first occurrence of value x.
Example: [Link]('apple')
pop([i])
Removes and returns item at index i. Default is last item.
Example: [Link](0)

index(x)
Returns the index of the first occurrence of x.
Example: [Link]('banana')

count(x)
Counts how many times x appears in the list.
Example: [Link]('apple')

sort()
Sorts the list in ascending order.
Example: [Link]()

sorted()
Built-in function: returns a new sorted list without changing the original.
Example: sorted(lst)

reverse()
Reverses the order of items in the list.
Example: [Link]()

copy()
Creates a shallow copy of the list.
Example: new_lst = [Link]()

clear()
Removes all items from the list.
Example: [Link]() → []

len()
Returns number of items in list.
Example: len(lst) → 5

max() / min()
Returns maximum or minimum element in a list.
Example: max([1,2,3]) → 3

sum()
Returns sum of numeric elements.
Example: sum([1,2,3]) → 6
slicing
Access a portion of the list using [:].
Example: lst[0:2] → first two items

enumerate()
Returns index and value when looping.
Example: for i, v in enumerate(lst): print(i, v)

3. Example: Fetching Data from an API


Suppose we call a dictionary API and get this response:

[
{
'word': 'example',
'meanings': [
{'partOfSpeech': 'noun', 'definitions': [{'definition': 'A representative form.'}]}
]
}
]

Here is how dictionary and list methods help us:

- data[0]['word'] → accesses the word 'example' (list index + dictionary key)

- data[0].keys() → shows all keys like 'word', 'meanings'

- data[0]['meanings'][0]['partOfSpeech'] → gets 'noun' (list + dict chain)

- for meaning in data[0]['meanings']: → loop through all meanings

- len(data[0]['meanings']) → tells how many meanings exist

Conclusion
Mastering dictionary and list methods allows you to easily handle JSON data from APIs.
Dictionaries are used to work with key-value mappings, while lists help manage multiple
ordered items. By combining both, you can efficiently fetch, process, and present API
responses in Python.

You might also like