Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to get first and last element from a Dictionary
Python dictionaries are ordered collections (as of Python 3.7+) that store data as key-value pairs. When working with dictionaries, you might need to access the first and last elements. This article demonstrates multiple methods to retrieve these elements efficiently.
Dictionary Basics
A dictionary is a collection of items that are unique, changeable, and ordered. Since Python 3.7, dictionaries maintain insertion order, meaning items retain the order in which they were added.
# Example dictionary
student_data = {'Name': 'Akash', 'Roll': '3', 'Subject': 'Bio'}
print(student_data)
{'Name': 'Akash', 'Roll': '3', 'Subject': 'Bio'}
Using list() and keys() Methods
This approach converts dictionary keys to a list, then accesses elements by index. Use index 0 for the first key and index -1 for the last key.
student_data = {'Name': 'Akash', 'Roll': '3', 'Subject': 'Bio'}
# Get first and last keys
first_key = list(student_data.keys())[0]
last_key = list(student_data.keys())[-1]
print("First key:", first_key)
print("First value:", student_data[first_key])
print("Last key:", last_key)
print("Last value:", student_data[last_key])
First key: Name First value: Akash Last key: Subject Last value: Bio
Complete Example with Multiple Elements
fruits = {'Apple': 1, 'Banana': 2, 'Orange': 3, 'Mango': 4, 'Grapes': 5}
print("Dictionary:", fruits)
first_key = list(fruits.keys())[0]
last_key = list(fruits.keys())[-1]
print("First key:", first_key, "| Value:", fruits[first_key])
print("Last key:", last_key, "| Value:", fruits[last_key])
Dictionary: {'Apple': 1, 'Banana': 2, 'Orange': 3, 'Mango': 4, 'Grapes': 5}
First key: Apple | Value: 1
Last key: Grapes | Value: 5
Using next() and iter() for First Element
For better performance when you only need the first element, use next(iter()). This method has O(1) complexity compared to O(n) for the list conversion method.
fruits = {'Apple': 1, 'Banana': 2, 'Orange': 3, 'Mango': 4, 'Grapes': 5}
print("Dictionary:", fruits)
# Get first key efficiently
first_key = next(iter(fruits))
first_value = fruits[first_key]
print("First key:", first_key)
print("First value:", first_value)
Dictionary: {'Apple': 1, 'Banana': 2, 'Orange': 3, 'Mango': 4, 'Grapes': 5}
First key: Apple
First value: 1
Method Comparison
| Method | Time Complexity | Best For |
|---|---|---|
list(dict.keys())[0] |
O(n) | Getting both first and last elements |
next(iter(dict)) |
O(1) | Getting only the first element |
Practical Example
def get_first_last_elements(dictionary):
if not dictionary:
return None, None
keys = list(dictionary.keys())
first_key = keys[0]
last_key = keys[-1]
return (first_key, dictionary[first_key]), (last_key, dictionary[last_key])
# Test the function
data = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5}
first, last = get_first_last_elements(data)
print("First element:", first)
print("Last element:", last)
First element: ('Monday', 1)
Last element: ('Friday', 5)
Conclusion
Use list(dict.keys())[0] and list(dict.keys())[-1] when you need both first and last elements. For better performance when accessing only the first element, use next(iter(dict)) which provides O(1) complexity.
