Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Ways to Convert List of ASCII Value to String - Python
Next article icon

Convert String List to ASCII Values – Python

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We need to convert each character into its corresponding ASCII value. For example, consider the list [“Hi”, “Bye”]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let’s discuss multiple ways to achieve this.

Using List Comprehension with ord()

A simple and efficient way to convert strings to ASCII values is by using list comprehension along with the ord() function.

Python
a = ["Hi", "Bye"] b = [[ord(c) for c in s] for s in a] print(b) 

Explanation:

  • The ord() function returns the ASCII value of a character.
  • We use a nested list comprehension to iterate through each string and convert its characters.
  • This method is concise and runs efficiently.

Let’s explore some more ways of converting string list to ascii values in Python.

Table of Content

  • Using map() with ord()
  • Using itertools.chain with map()
  • Using Dictionary Comprehension
  • Using for Loop

Using map() with ord()

Another efficient approach is using the map() function, which applies ord() to each character in a string.

Python
a = ["Hi", "Bye"] b = [list(map(ord, s)) for s in a] print(b) 

Explanation:

  • The map() function applies ord() to each character in the string.
  • Since map() returns a map object, we convert it into a list.
  • This method is similar in efficiency to list comprehension but can be slightly more readable for some.

Using itertools.chain with map()

If we need a flat list of ASCII values instead of a nested list, we can use itertools.chain().

Python
from itertools import chain  a = ["Hi", "Bye"] b = list(map(ord, chain(*a))) print(b) 

Explanation:

  • chain(*a) flattens the list, treating all characters as a single sequence.
  • map(ord, …) applies ord() to each character.
  • This method is useful when we want a flat list instead of a list of lists.

Using Dictionary Comprehension

If we want to keep track of original characters, we can store them in a dictionary.

Python
a = ["Hi", "Bye"] b = [{c: ord(c) for c in s} for s in a] print(b) 

Explanation:

  • Instead of just converting characters, this stores them as {char: ASCII} pairs.
  • This method helps when we need both original characters and their ASCII values.

Using for Loop

We can use a standard for loop to iterate through each character in the list and convert it to ASCII values.

Python
a = ["Hi", "Bye"] b = [] for s in a:     temp = []     for c in s:         temp.append(ord(c))     b.append(temp) print(b) 

Explanation:

  • We initialize an empty list b to store the converted values.
  • We loop through each string in the list and convert each character to ASCII using ord().


Next Article
Ways to Convert List of ASCII Value to String - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • ASCII
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Ways to Convert List of ASCII Value to String - Python
    The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting
    3 min read
  • Python | Convert String to list of tuples
    Sometimes, while working with data, we can have a problem in which we have a string list of data and we need to convert the same to list of records. This kind of problem can come when we deal with a lot of string data. Let's discuss certain ways in which this task can be performed. Method #1: Using
    8 min read
  • Convert Float String List to Float Values-Python
    The task of converting a list of float strings to float values in Python involves changing the elements of the list, which are originally represented as strings, into their corresponding float data type. For example, given a list a = ['87.6', '454.6', '9.34', '23', '12.3'], the goal is to convert ea
    3 min read
  • Python | Convert string tuples to list tuples
    Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this tas
    4 min read
  • Convert tuple to string in Python
    The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
    3 min read
  • Python Program to Convert a List to String
    In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it. Using th
    3 min read
  • Convert String to Tuple - Python
    When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
    2 min read
  • Convert Dictionary values to Strings
    In Python, dictionaries store key-value pairs, where values can be of any data type. Sometimes, we need to convert all the values of a dictionary into strings. For example, given the dictionary {'a': 1, 'b': 2.5, 'c': True}, we might want to convert the values 1, 2.5, and True to their string repres
    3 min read
  • Convert List of Tuples to List of Strings - Python
    The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings. For example, gi
    3 min read
  • Convert String to bytes-Python
    The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Let’s explore different method
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences