- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Find the sum of Length of Strings at given indices
When it is required to find the sum of the length of string at specific indices, the ‘enumerate’ is used to iterate through the elements in the list and adding the length of the element to a list.
Example
Below is a demonstration of the same
my_list = ["python", "is", "best", "for", "coders"] print("The list is :") print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list: result += len(element) print("The result is :") print(result)
Output
The list is : ['python', 'is', 'best', 'for', 'coders'] The result is : 14
Explanation
A list is defined (which contains strings) and is displayed on the console.
Another list with integers is defined.
The list of strings is enumerated and iterated over.
A variable is assigned to 0.
If that index is present in the list of integers, its length is added to the variable.
This is the result which is displayed on the console.
- Related Articles
- Python - Find all the strings that are substrings to the given list of strings
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- Program to get maximum length merge of two given strings in Python
- Find all good indices in the given Array in Python
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- Program to find length of longest common subsequence of three strings in Python
- Find indices with None values in given list in Python
- Find sum of frequency of given elements in the list in Python
- Find the GCD of N Fibonacci Numbers with given Indices in C++
- Count of sub-strings of length n possible from the given string in C++
- Program to find the sum of all digits of given number in Python
- Program to find sum of all odd length subarrays in Python
- Find all distinct palindromic sub-strings of a given String in Python
- Python - Ways to find indices of value in list

Advertisements