Fixing Text Storing/Loading

You should write each list from get_user_list with a new line generally when storing as text. Also note you don't need to call f.close() when using with as it closes the file for you when it is no longer needed.

with open(str(epoch_time) + '_intercom_Users.txt', 'w') as f:
    for item in get_users_list:
        f.write(f"{item}\n")

This will allow each item to be read in separately and not have to deal with trying to split up your string later.

Then when you go to read you will have a string representation of a list on each line. You need to do a literal eval and remove your new lines characters as well. You can do this in one list comprehension.

import ast

with open('1556640109_intercom_Users.txt', 'r') as f:
    x = f.readlines()   # read into a list
    x = [ast.literal_eval(x[i].rstrip('\n')) for i in range(len(x))]

It would seem like you don't need the \n since we add it then remove it, but it's an easy way to make each item remain a separate item. Without that you would need to add a separator and read in the single line then do a .split(). Using this method will allow you to read the data at rest easily and read it back in easily as well.


Using Pickle Instead

As others have noted, this isn't a great way to do data serialization which is what it appears you are doing. Python comes preloaded with pickle which can serialize and store any Python data type as is and then read it back it.

You could use it like:

import pickle

def get_users():
    counter = 0
    for i in users_intercom:
        get_users_list.append(users_intercom[counter].split())
        counter = counter + 1

        if counter > 100:
            with open(str(epoch_time) + '_intercom_Users.pickle', 'wb') as f:
                pickle.dump(get_users_list, f)
            break

And then read it again:

with open('1556640109_intercom_Users', 'rb') as f:
    x = pickle.load(f)
Answer from MyNameIsCaleb on Stack Overflow
Top answer
1 of 5
3

Fixing Text Storing/Loading

You should write each list from get_user_list with a new line generally when storing as text. Also note you don't need to call f.close() when using with as it closes the file for you when it is no longer needed.

with open(str(epoch_time) + '_intercom_Users.txt', 'w') as f:
    for item in get_users_list:
        f.write(f"{item}\n")

This will allow each item to be read in separately and not have to deal with trying to split up your string later.

Then when you go to read you will have a string representation of a list on each line. You need to do a literal eval and remove your new lines characters as well. You can do this in one list comprehension.

import ast

with open('1556640109_intercom_Users.txt', 'r') as f:
    x = f.readlines()   # read into a list
    x = [ast.literal_eval(x[i].rstrip('\n')) for i in range(len(x))]

It would seem like you don't need the \n since we add it then remove it, but it's an easy way to make each item remain a separate item. Without that you would need to add a separator and read in the single line then do a .split(). Using this method will allow you to read the data at rest easily and read it back in easily as well.


Using Pickle Instead

As others have noted, this isn't a great way to do data serialization which is what it appears you are doing. Python comes preloaded with pickle which can serialize and store any Python data type as is and then read it back it.

You could use it like:

import pickle

def get_users():
    counter = 0
    for i in users_intercom:
        get_users_list.append(users_intercom[counter].split())
        counter = counter + 1

        if counter > 100:
            with open(str(epoch_time) + '_intercom_Users.pickle', 'wb') as f:
                pickle.dump(get_users_list, f)
            break

And then read it again:

with open('1556640109_intercom_Users', 'rb') as f:
    x = pickle.load(f)
2 of 5
2

As already noted you need to add newlines (\n). As you tagged your question with Python3, then you should be able to employ so-called f-strings. That is:

for item in get_users_list:
    f.write(f"{item}\n")

If you wish to know more about f-strings you might read this tutorial. Others method of string formatting exist too, so feel free to use that which suits your needs best.

EDIT: Now I readed that you need access to particular items of lists. It is possible to get it working using just text writing to and reading from files, however I suggest you to examine already developed modules for data serialization.

🌐
W3Schools
w3schools.com β€Ί python β€Ί python_lists.asp
Python Lists
List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members. Dictionary is a collection which is ordered** and changeable. No duplicate members. *Set items are unchangeable, but you can remove and/or add items whenever you like. **As of Python version 3.7, dictionaries are ordered.
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί datastructures.html
5. Data Structures β€” Python 3.14.3 documentation
This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method...
🌐
Google
developers.google.com β€Ί google for education β€Ί python β€Ί python lists
Python Lists | Python Education | Google for Developers
January 23, 2026 - You may have habits from other languages where you start manually iterating over a collection, where in Python you should just use for/in. You can also use for/in to work on a string. The string acts like a list of its chars, so for ch in s: print(ch) prints all the chars in a string.
🌐
Programiz
programiz.com β€Ί python-programming β€Ί list
Python List (With Examples)
December 30, 2025 - Python lists store multiple data together in a single variable. In this tutorial, we will learn about Python lists (creating lists, changing list items, removing items, and other list operations) with the help of examples.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop.
Published Β  December 27, 2025
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί reading-and-writing-lists-to-a-file-in-python
Reading and Writing lists to a file in Python - GeeksforGeeks
July 23, 2025 - To read the JSON data back into Python as a dictionary or list, you can use the json.load() function.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί python_lists.htm
Python - Lists
A Python list is mutable. Any item from the list can be accessed using its index, and can be modified. One or more objects from the list can be removed or added.
🌐
Python.org
discuss.python.org β€Ί python help
How to use the list() function on a text file, to make turn the text file into a list of characters? - Python Help - Discussions on Python.org
April 25, 2024 - How would i do that? #The code f = open('/storage/emulated/0/files/text.txt', 'r') text = f.read() char = list(text) print(char) Heres the text file abcdef I want the result to be: [β€˜a’, β€˜b’, β€˜c’, β€˜d’, β€˜e’, β€˜f’] B…
Top answer
1 of 8
361
with open('C:/path/numbers.txt') as f:
    lines = f.read().splitlines()

this will give you a list of values (strings) you had in your file, with newlines stripped.

In Python 3.5+, you can also do:

lines = pathlib.Path('C:/path/numbers.txt').read_text().splitlines()

also, watch your backslashes in windows path names, as those are also escape chars in strings. You can use forward slashes or double backslashes instead.

2 of 8
133

Two ways to read file into list in python (note these are not either or) -

  1. use of with - supported from python 2.5 and above
  2. use of list comprehensions

1. use of with

This is the pythonic way of opening and reading files.

#Sample 1 - elucidating each step but not memory efficient
lines = []
with open("C:\name\MyDocuments\numbers") as file:
    for line in file: 
        line = line.strip() #or some other preprocessing
        lines.append(line) #storing everything in memory!

#Sample 2 - a more pythonic and idiomatic way but still not memory efficient
with open("C:\name\MyDocuments\numbers") as file:
    lines = [line.strip() for line in file]

#Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators. 
with open("C:\name\MyDocuments\numbers") as file:
    for line in file:
        line = line.strip() #preprocess line
        doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed.

the .strip() is used for each line of the file to remove \n newline character that each line might have. When the with ends, the file will be closed automatically for you. This is true even if an exception is raised inside of it.

2. use of list comprehension

This could be considered inefficient as the file descriptor might not be closed immediately. Could be a potential issue when this is called inside a function opening thousands of files.

data = [line.strip() for line in open("C:/name/MyDocuments/numbers", 'r')]

Note that file closing is implementation dependent. Normally unused variables are garbage collected by python interpreter. In cPython (the regular interpreter version from python.org), it will happen immediately, since its garbage collector works by reference counting. In another interpreter, like Jython or Iron Python, there may be a delay.

🌐
Quora
quora.com β€Ί How-do-you-read-a-list-in-Python
How to read a list in Python - Quora
Python is an especially good choice if you want to get started quickly with coding and don’t need to worry about making your code as efficient as possible. ... 1) Create the list using list_name = [item1, item2]. You can append items to the end of the list by using: list_name.append(item). 2) To read the value of the first element of the list, in this case [1, 2, 3], you would write:
🌐
Stack Abuse
stackabuse.com β€Ί reading-and-writing-lists-to-a-file-in-python
Reading and Writing Lists to a File in Python
September 20, 2022 - Python programmers intensively use arrays, lists, and dictionaries as serialized data structures. Storing these data structures persistently requires either a file or a database to properly work. In this article, we'll take a look at how to write a list to file, and how to read that list back ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-get-a-list-as-input-from-user
Get a list as input from user in Python - GeeksforGeeks
a = [] # Get the number of elements n = int(input("Enter the number of elements: ")) # Append elements to the list for i in range(n): element = input(f"Enter element {i+1}: ") a.append(element) print("List:", a) ... Enter the number of elements: 3 Enter element 1: Python Enter element 2 : is Enter element 3: fun List: ['Python', 'is', 'fun']
Published Β  December 5, 2024
🌐
Python.org
discuss.python.org β€Ί python help
How can I get a value at any position of a list - Python Help - Discussions on Python.org
September 30, 2022 - I have a numbers added in the list, and want to get a value at a particular number from the list. I tried one = (open(input("Open list: ")).read().splitlines()) list = one print(list) for i in range(len(list)): …
🌐
W3Schools
w3schools.com β€Ί python β€Ί gloss_python_access_list_items.asp
Python Access List Items
Specify negative indexes if you want to start the search from the end of the list: This example returns the items from index -4 (included) to index -1 (excluded) thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[-4:-1]) Try it Yourself Β» Β· Python Lists Tutorial Lists Change List Item Loop List Items List Comprehension Check If List Item Exists List Length Add List Items Remove List Items Copy a List Join Two Lists
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-accessing-index-and-value-in-list
Accessing index and value in Python list - GeeksforGeeks
May 9, 2025 - For example, using enumerate(list) in a loop like for index, value in enumerate(list) allows us to access both the index and the value together. enumerate() is preferred and most efficient method for accessing both index and value in Python.
🌐
Edureka
edureka.co β€Ί blog β€Ί input-a-list-in-python
How To Input A List In Python | Python Lists - Edureka
February 13, 2025 - This article will introduce you to different ways to input a list in Python and give you a detailed programmatic demonstration. Read this blog now!
🌐
Studytonight
studytonight.com β€Ί python-howtos β€Ί how-to-read-element-in-python-list
How to Read Element in Python List - Studytonight
#Intializing list list=["python",10,0.7895,True,50.2145,100] print("Elements present in list are:",list) #Reading elements of list by index method.