Join Lists

 
0
 

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?

Featured Replies in this Discussion

  • 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: ...
    Post Contains: Code
  • So, what will this do? ...
    Post Contains: Code
  • So, you have a filename called ... which looks kinda like this: ... You could make all that a list of strings, than work with them: ... Or, you could parse the entire file, line by line, and append your items to the list, also, converting them to int. Useful links: ... ... Good luck.
    Post Contains: Links Code
  • Some more ways,i use ... as shown by Lucaci. ... So we get a list of numbers that are strings. To get a list with integer or float,use ... or ... ... Rembere output is always ... when you read from file. Can test with ... and output is as expected. ...
    Post Contains: Code
 
0
 

You can join lists like this:

listAB = listA + listB

 
0
 

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.

 
1
 

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)
 
1
 

So, what will this do?

with open('numbers.txt') as f:
    for line in f:
        print(line, type(line))
 
0
 
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'>
>>> 
 
1
 

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.

 
2
 

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
 
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]
'''
 
0
 

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]
'''
Question Answered as of 2 Years Ago by Lucaci Andrew, Walara, vegaseat and 3 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article