Thursday, 23 January 2025

Day 98 : Python Program to Create a Dictionary with Key as First Character and Value as Words Starting


 words = ['apple', 'banana', 'avocado', 'berry', 'cherry', 'apricot']

word_dict = {}

for word in words:

    first_char = word[0].lower()  

    if first_char in word_dict:

        word_dict[first_char].append(word)  

    else:

        word_dict[first_char] = [word]  

        print("Dictionary with first character as key:", word_dict)

#source code --> clcoding.com 

Code Explanation:

Input List:

words = ['apple', 'banana', 'avocado', 'berry', 'cherry', 'apricot']

This is the list of words that we want to group based on their first characters.

Create an Empty Dictionary:

word_dict = {}

This dictionary will hold the first characters of the words as keys and lists of corresponding words as values.

Iterate Through the Words:

for word in words:

The loop goes through each word in the words list one by one.

Extract the First Character:

first_char = word[0].lower()

word[0] extracts the first character of the current word.

.lower() ensures the character is in lowercase, making the process case-insensitive (useful if the words had uppercase letters).

Check if the First Character is in the Dictionary:

if first_char in word_dict:

This checks if the first character of the current word is already a key in word_dict.

Append or Create a New Key:

If the Key Exists:

word_dict[first_char].append(word)

The current word is added to the existing list of words under the corresponding key.

If the Key Does Not Exist:

word_dict[first_char] = [word]

A new key-value pair is created in the dictionary, where the key is the first character, and the value is a new list containing the current word.

Output the Dictionary:

print("Dictionary with first character as key:", word_dict)

This prints the resulting dictionary, showing words grouped by their first characters.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (95) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (184) C (77) C# (12) C++ (83) Course (67) Coursera (236) Cybersecurity (25) Data Analytics (2) data management (11) Data Science (138) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (22) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (5) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (67) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (965) Python Coding Challenge (411) Python Quiz (61) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses