Accessing items of an OrderedDict in Python - Complete guide

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
12 min

banner

# Table of Contents

  1. Get the First element of an OrderedDict in Python
  2. Get the Last element of an OrderedDict in Python
  3. Get all keys in an OrderedDict as a list in Python
  4. Access items in an OrderedDict by index in Python
  5. Merge two OrderedDict objects in Python
  6. Get the index of Key or Value in an OrderedDict in Python
  7. Convert OrderedDict to regular Dict or List in Python.

# Get the First element of an OrderedDict in Python

To get the first element of an OrderedDict:

  1. Use the iter() function to create an iterator object from the OrderedDict.
  2. Pass the iterator object to the next() function to get the first item of the OrderedDict.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) first_key = next(iter(my_dict)) print(first_key) # ๐Ÿ‘‰๏ธ name first_value = my_dict[first_key] print(first_value) # ๐Ÿ‘‰๏ธ bobbyhadz first_pair = next(iter(my_dict.items())) print(first_pair) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(list(my_dict.items())[0]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(list(my_dict.items())[1]) # ๐Ÿ‘‰๏ธ ('age', 30)

get first element of ordered dict

The code for this article is available on GitHub

The iter() function takes an object and constructs an iterator from it.

The next() function returns the next item from the provided iterator.

You can pass the result of calling dict.items() to the iter() function if you need to get the first key-value pair of the OrderedDict.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) first_key = next(iter(my_dict)) print(first_key) # ๐Ÿ‘‰๏ธ name first_value = my_dict[first_key] print(first_value) # ๐Ÿ‘‰๏ธ bobbyhadz first_pair = next(iter(my_dict.items())) print(first_pair) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz')
The code for this article is available on GitHub

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # ๐Ÿ‘‡๏ธ odict_items([('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')]) print(my_dict.items())

You can convert the view object to a list if you need to access a specific index in the OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) print(list(my_dict.items())[0]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(list(my_dict.items())[1]) # ๐Ÿ‘‰๏ธ ('age', 30)
Converting the view object to a list is necessary because view objects are not subscriptable (cannot be accessed at a specific index).

You can use the reversed() method if you need to get the last element in an OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) last_key = next(reversed(my_dict)) print(last_key) # ๐Ÿ‘‰๏ธ topic last_value = my_dict[last_key] print(last_value) # ๐Ÿ‘‰๏ธ Python last_pair = next(reversed(my_dict.items())) print(last_pair) # ๐Ÿ‘‰๏ธ ('topic', 'Python')
The code for this article is available on GitHub

The reversed function takes an iterator, reverses it and returns the result.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # ๐Ÿ‘‡๏ธ ['topic', 'age', 'name'] print(list(reversed(my_dict)))

Note that the next() function can be passed a default value as the second argument.

main.py
from collections import OrderedDict my_dict = OrderedDict() first_pair = next(iter(my_dict.items()), None) print(first_pair) # ๐Ÿ‘‰๏ธ None if first_pair is not None: print('Do work')

If the iterator is exhausted or empty, the default value is returned.

If the iterator is exhausted or empty and no default value is provided, aStopIteration exception is raised.

You can also use a basic for loop to get the first item of an OrderedDict.

# Get the First element of an OrderedDict using a for loop

This is a three-step process:

  1. Use a for loop to iterate over the OrderedDict.
  2. Store the first key-value pair in a variable.
  3. Use the break statement to exit out of the loop on the first iteration.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) for key in my_dict: print(key) # ๐Ÿ‘‰๏ธ name print(my_dict[key]) # ๐Ÿ‘‰๏ธ bobbyhadz break

get first element of ordered dict using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the OrderedDict once.

The break statement breaks out of the innermost enclosing for or while loop.

After we get the first key-value pair of the OrderedDict, we exit the for loop.

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

# Table of Contents

  1. Get the Last element of an OrderedDict in Python
  2. Get all keys in an OrderedDict as a list in Python
  3. Access items in an OrderedDict by index in Python
  4. Merge two OrderedDict objects in Python
  5. Get the index of Key or Value in an OrderedDict in Python
  6. Convert OrderedDict to regular Dict or List in Python.

# Get the last element of an OrderedDict in Python

To get the last element of an OrderedDict:

  1. Use the reversed() function to reverse the OrderedDict object.
  2. Pass the result to the next() function to get the last element of the OrderedDict.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) last_key = next(reversed(my_dict)) print(last_key) # ๐Ÿ‘‰๏ธ topic last_value = my_dict[last_key] print(last_value) # ๐Ÿ‘‰๏ธ Python last_pair = next(reversed(my_dict.items())) print(last_pair) # ๐Ÿ‘‰๏ธ ('topic', 'Python')
The code for this article is available on GitHub

The reversed() function takes an iterator, reverses it and returns the result.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) # ๐Ÿ‘‡๏ธ ['topic', 'name', 'id'] print(list(reversed(my_dict)))

get last element of ordered dict

The next() function returns the next item from the provided iterator.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) last_key = next(reversed(my_dict)) print(last_key) # ๐Ÿ‘‰๏ธ topic last_value = my_dict[last_key] print(last_value) # ๐Ÿ‘‰๏ธ Python last_pair = next(reversed(my_dict.items())) print(last_pair) # ๐Ÿ‘‰๏ธ ('topic', 'Python')

You can pass the result of calling the dict.items() method to the reversed() function to get the last key-value pair of the OrderedDict.

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) # ๐Ÿ‘‡๏ธ odict_items([('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')]) print(my_dict.items())

You can convert the view object to a list if you need to access a specific index in the OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) print(list(my_dict.items())[0]) # ๐Ÿ‘‰๏ธ ('id', 1) print(list(my_dict.items())[1]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz')
The code for this article is available on GitHub
This is necessary because view objects are not subscriptable (cannot be accessed at a specific index).

You can use the same approach to get the first element in an OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'bobbyhadz'), ('topic', 'Python')] ) first_key = next(iter(my_dict)) print(first_key) # ๐Ÿ‘‰๏ธ id first_value = my_dict[first_key] print(first_value) # ๐Ÿ‘‰๏ธ 1 first_pair = next(iter(my_dict.items())) print(first_pair) # ๐Ÿ‘‰๏ธ ('id', 1)

We used the iter() function to get an iterator object.

It should be noted that the next() function can be passed a default value as the second argument.
main.py
from collections import OrderedDict my_dict = OrderedDict() last_key = next(reversed(my_dict), 'default value') print(last_key) # ๐Ÿ‘‰๏ธ default value

If the iterator is exhausted or empty, the default value is returned.

If the iterator is exhausted or empty and no default value is provided, a StopIteration exception is raised.

If your OrderedDict might be empty, you could supply a default value of None if you need to avoid the StopIteration exception.
main.py
from collections import OrderedDict my_dict = OrderedDict() last_pair = next(reversed(my_dict.items()), None) print(last_pair) # ๐Ÿ‘‰๏ธ None if last_pair is not None: print('Do work')

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

# Table of Contents

  1. Get all keys in an OrderedDict as a list in Python
  2. Access items in an OrderedDict by index in Python
  3. Merge two OrderedDict objects in Python
  4. Get the index of Key or Value in an OrderedDict in Python
  5. Convert OrderedDict to regular Dict or List in Python.

# Get all keys in an OrderedDict as a list in Python

To get all keys in an OrderedDict() as a list:

  1. Use the dict.keys() method to get a view object of the keys in the OrderedDict.
  2. Use the list() class to convert the view object to a list.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) all_keys = list(my_dict.keys()) print(all_keys) # ๐Ÿ‘‰๏ธ ['first', 'last', 'age'] all_values = list(my_dict.values()) print(all_values) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 30] all_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] print(all_items)

get all keys in ordered dict as list

The code for this article is available on GitHub

The dict.keys() method returns a new view of the dictionary's keys.

The dict.keys() method returns a view object, so we used the list() class to convert the view to a list.

This is necessary if you need to access specific keys by index.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) all_keys = list(my_dict.keys()) print(all_keys) # ๐Ÿ‘‰๏ธ ['first', 'last', 'age'] print(all_keys[0]) # ๐Ÿ‘‰๏ธ first print(all_keys[1]) # ๐Ÿ‘‰๏ธ last

You can use the dict.values() method if you need to get all values in the OrderedDict as a list.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) all_values = list(my_dict.values()) print(all_values) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 30] print(all_values[0]) # ๐Ÿ‘‰๏ธ bobby print(all_values[1]) # ๐Ÿ‘‰๏ธ hadz

The dict.values method returns a new view of the dictionary's values.

You can also use the dict.items() method if you need to get all key-value pairs in the OrderedDict as a list.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) all_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] print(all_items) print(all_items[0]) # ๐Ÿ‘‰๏ธ ('first', 'bobby') print(all_items[0][0]) # ๐Ÿ‘‰๏ธ first print(all_items[0][1]) # ๐Ÿ‘‰๏ธ bobby

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

You can also use list slicing if you need to get a slice of the keys, values or items in the dictionary.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) all_keys = list(my_dict.keys()) print(all_keys) # ๐Ÿ‘‰๏ธ ['first', 'last', 'age'] first_2 = all_keys[:2] print(first_2) # ๐Ÿ‘‰๏ธ ['first', 'last']

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the list.

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

# Table of Contents

  1. Access items in an OrderedDict by index in Python
  2. Merge two OrderedDict objects in Python
  3. Get the index of Key or Value in an OrderedDict in Python
  4. Convert OrderedDict to regular Dict or List in Python.

# Access items in an OrderedDict by index in Python

To access items in an OrderedDict by index:

  1. Use the dict.items() method to get a view of the dictionary's items.
  2. Use the list() class to convert the view object to a list.
  3. Access the list at a specific index.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(dict_items) print(dict_items[0]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(dict_items[0][0]) # ๐Ÿ‘‰๏ธ name print(dict_items[0][1]) # ๐Ÿ‘‰๏ธ bobbyhadz

access items in ordered dict by index

The code for this article is available on GitHub

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # ๐Ÿ‘‡๏ธ odict_items([('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')]) print(my_dict.items())
However, view objects are not subscriptable (cannot be accessed at a specific index), so we had to convert the view object to a list.

Once we have a list of the items of the OrderedDict, we can access the list at a specific index.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_items = list(my_dict.items()) print(dict_items[0]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(dict_items[0][0]) # ๐Ÿ‘‰๏ธ name print(dict_items[0][1]) # ๐Ÿ‘‰๏ธ bobbyhadz
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

You can also use the dict.keys() or dict.values() method if you only need to access a key or a value at a specific index.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_keys = list(my_dict.keys()) print(dict_keys) # ๐Ÿ‘‰๏ธ ['name', 'age', 'topic'] print(dict_keys[1]) # ๐Ÿ‘‰๏ธ age dict_values = list(my_dict.values()) print(dict_values) # ๐Ÿ‘‰๏ธ ['bobbyhadz', 30, 'Python'] print(dict_values[1]) # ๐Ÿ‘‰๏ธ 30

The dict.keys() method returns a new view of the dictionary's keys.

The dict.values() method returns a new view of the dictionary's values.

You can use the list.index() method if you need to get the index of a specific key in the OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_keys = list(my_dict.keys()) index = dict_keys.index('topic') print(index) # ๐Ÿ‘‰๏ธ 2 dict_values = list(my_dict.values()) print(dict_values[index]) # ๐Ÿ‘‰๏ธ Python

The list.index() method returns the index of the first item whose value is equal to the provided argument.

You can use list slicing if you need to get a slice of the dictionary's items.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(dict_items) first_2 = dict_items[:2] print(first_2) # ๐Ÿ‘‰๏ธ [('name', 'bobbyhadz'), ('age', 30)]
The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the list.

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

# Table of Contents

  1. Merge two OrderedDict objects in Python
  2. Get the index of Key or Value in an OrderedDict in Python
  3. Convert OrderedDict to regular Dict or List in Python.

# Merge two OrderedDict objects in Python

To merge two OrderedDict objects:

  1. Use the dict.items() method to get a view of the items of the objects.
  2. Use the list() class to convert the view objects to lists.
  3. Merge the objects in the call to the OrderedDict() class.
main.py
from collections import OrderedDict od1 = OrderedDict([('first', 'bobby')]) od2 = OrderedDict([('last', 'hadz'), ('age', 30)]) od3 = OrderedDict(list(od1.items()) + list(od2.items())) # ๐Ÿ‘‡๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(od3)
The code for this article is available on GitHub

We used the dict.items() method to get a view of the items of the OrderedDict objects

main.py
from collections import OrderedDict od1 = OrderedDict([('first', 'bobby')]) # ๐Ÿ‘‡๏ธ odict_items([('first', 'bobby')]) print(od1.items()) od2 = OrderedDict([('last', 'hadz'), ('age', 30)]) # ๐Ÿ‘‡๏ธ odict_items([('last', 'hadz'), ('age', 30)]) print(od2.items())

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

However, we can't use the addition (+) operator to combine view objects, so we had to convert them to lists.

When the addition (+) operator is used with two lists, it merges them.
main.py
# ๐Ÿ‘‡๏ธ [('first', 'bobby'), ('last', 'hadz')] print([('first', 'bobby')] + [('last', 'hadz')])

The last step is to pass the list of key-value pairs to the OrderedDict class.

main.py
from collections import OrderedDict od1 = OrderedDict([('first', 'bobby')]) od2 = OrderedDict([('last', 'hadz'), ('age', 30)]) od3 = OrderedDict(list(od1.items()) + list(od2.items())) # ๐Ÿ‘‡๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(od3)

# Merge two OrderedDict objects using dict.update()

Alternatively, you can use the dict.update() method.

main.py
from collections import OrderedDict od1 = OrderedDict([('first', 'bobby')]) od2 = OrderedDict([('last', 'hadz'), ('age', 30)]) od1.update(od2) # ๐Ÿ‘‡๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(od1)
The code for this article is available on GitHub

The dict.update method updates the dictionary with the key-value pairs from the provided value.

main.py
my_dict = {'name': 'Alice'} my_dict.update({'name': 'bobbyhadz', 'age': 30}) print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'bobbyhadz', 'age': 30}

The method overrides the dictionary's existing keys and returns None.

This approach updates the first OrderedDict in place.

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

# Table of Contents

  1. Get the index of Key or Value in an OrderedDict in Python
  2. Convert OrderedDict to regular Dict or List in Python.

# Get the index of Key or Value in an OrderedDict in Python

To get the index of a key or a value in an OrderedDict:

  1. Use the dict.keys() or dict.values() methods to get a view of the dictionary's keys or values.
  2. Use the list() class to convert the view to a list.
  3. Use the list.index() method to get the index of the key or value.
main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # โœ… Get index of key in an OrderedDict dict_keys = list(my_dict.keys()) print(dict_keys) # ๐Ÿ‘‰๏ธ ['name', 'age', 'topic'] index = dict_keys.index('age') print(index) # ๐Ÿ‘‰๏ธ 1 # ----------------------------------------- # โœ… Get the index of a value in an OrderedDict value = 'Python' dict_values = list(my_dict.values()) print(dict_values) # ๐Ÿ‘‰๏ธ ['bobbyhadz', 30, 'Python'] index = dict_values.index('Python') print(index) # ๐Ÿ‘‰๏ธ 2
The code for this article is available on GitHub

We used the dict.keys() and dict.values() methods to get a view of the dictionary's keys and values.

We cannot call the list.index() method on a view object, so we had to convert the objects to lists.

The list.index() method returns the index of the first item whose value is equal to the provided argument.

The method raises a ValueError if there is no such item in the list.

You can use a try/except statement if you need to handle the error.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_keys = list(my_dict.keys()) print(dict_keys) # ๐Ÿ‘‰๏ธ ['name', 'age', 'topic'] try: index = dict_keys.index('another') except ValueError: # ๐Ÿ‘‡๏ธ This runs print('The specific key not is not in the OrderedDict')

You can use the dict.items() method if you need to get a key-value pair of the OrderedDict by index.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(dict_items) print(dict_items[0]) # ๐Ÿ‘‰๏ธ ('name', 'bobbyhadz') print(dict_items[0][0]) # ๐Ÿ‘‰๏ธ name print(dict_items[0][1]) # ๐Ÿ‘‰๏ธ bobbyhadz

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

You can use list slicing if you need to get a slice of the list.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) dict_items = list(my_dict.items()) # ๐Ÿ‘‡๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(dict_items) print(dict_items[1:3]) # ๐Ÿ‘‰๏ธ [('age', 30), ('topic', 'Python')]
The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the list.

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The OrderedDict class is a subclass of dict that remembers the order in which its entries were added.

Note that as of Python 3.7, the standard dict class is guaranteed to preserve the insertion order as well.

I've also written an article on how to convert an OrderedDict to a regular Dict or List.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev