Hey all,

I am working on learning python, I have a question about lists.

When I call Variable x to equal numbers from a text file, I am getting the file returned in multiple lists.

with open('numbers.txt') as f:
    for item in f:
        n = item.split()
        print (n)

I end up with numbers in the format.

['1']
['2']
['3']

I want to join these numbers into one list. Unless i'm mistaken, I am calling variable n as this group of lists. When I try to combine I end up with errors.

Essentially, I want to eventually add these numbers, but I will get to that some other time.

Am I in the correct direction? Should i be reading these numbees a different way?

Recommended Answers

All 9 Replies

You can join lists like this:

listAB = listA + listB

The problem I am having is comprehending the fact that when I call variable n, I get it printed as two or more lists. I don't have it able to call them anything other than n right now.

Looks you have only one number per line so you do not need to loop line by line. I am not going ot show directly how to make them int egers instead of str ings, but this gets you the strings:

number_strings = list(f)

So, what will this do?

with open('numbers.txt') as f:
    for line in f:
        print(line, type(line))
1
 <class 'str'>
2
 <class 'str'>
3
 <class 'str'>
4
 <class 'str'>
5
 <class 'str'>
6
 <class 'str'>
7
 <class 'str'>
8
 <class 'str'>
9
 <class 'str'>
10 <class 'str'>
>>> 

So, you have a filename called 'numbers.txt' which looks kinda like this:

'''
1
2
3
4
5
6
7
8
'''

You could make all that a list of strings, than work with them:

l = []
with open('numbers.txt') as f:
    l = list(f)
print l

Or, you could parse the entire file, line by line, and append your items to the list, also, converting them to int.
Useful links:
List operations
The 'with' statement

Good luck.

Some more ways,i use numbers.txt as shown by Lucaci.

with open('numbers.txt') as f:
    print [i.strip() for i in f]

#['1', '2', '3', '4', '5', '6', '7', '8']

So we get a list of numbers that are strings.
To get a list with integer or float,use int() or float()

with open('numbers.txt') as f:
    print [float(i) for i in f]

#[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]

Rembere output is always strings when you read from file.
Can test with sum() and output is as expected.

with open('numbers.txt') as f:
    print sum(float(i) for i in f) #36.0

Just another way ...

with open('numbers.txt') as f:
    raw_list = f.readlines()

print raw_list

mylist = [eval(item) for item in raw_list]

print mylist

'''
['1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '8\n', '9\n', '10\n']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
'''

Or you could use the old fashion append style.

l = []
with open('numbers.txt') as f:
    for i in f: l.append(int(i))
print l

'''
[1, 2, 3, 4, 5, 6, 7, 8]
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.