🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Python, dictionaries are used to store data in key-value pairs. Unlike lists, dictionaries don't have an append method. However, adding new elements or appending to existing elements in a dictionary is a common task, which can be achieved using different methods.
Appending to a dictionary typically means adding a new key-value pair or updating the value associated with an existing key. If the value is a list, appending may mean adding a new item to the list.
2. Program Steps
1. Start with an existing dictionary.
2. Determine the key where the value should be appended or updated.
3. Check if the key exists and if its value is a list; then append to it. Otherwise, update or add the key-value pair.
4. Output the updated dictionary.
3. Code Program
# Initialize a dictionary with a list as a value
my_dict = {'numbers': [1, 2, 3], 'letters': ['a', 'b']}
# Append to the list under the 'numbers' key
my_dict['numbers'].append(4)
# Append a new key-value pair
my_dict['colors'] = ['red']
# Check if the 'letters' key exists and append, else create a new list
if 'letters' in my_dict:
my_dict['letters'].append('c')
else:
my_dict['letters'] = ['c']
# Print the updated dictionary
print(f"Updated dictionary: {my_dict}")
Output:
Updated dictionary: {'numbers': [1, 2, 3, 4], 'letters': ['a', 'b', 'c'], 'colors': ['red']}
Explanation:
1. my_dict is initialized with two keys: 'numbers' and 'letters', each with a list as its value.
2. .append(4) is called on the list associated with the 'numbers' key to add a new element.
3. A new key 'colors' with a list ['red'] as its value is added to my_dict.
4. An if statement checks if the key 'letters' exists. Since it does, .append('c') is called on its list.
5. print outputs my_dict, showing the appended values and the newly added key-value pair.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment