1,105,352 Community Members

Join Lists

scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
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?

Member Avatar
Walara
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

You can join lists like this:

listAB = listA + listB

scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
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.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
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)
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
1
 

So, what will this do?

with open('numbers.txt') as f:
    for line in f:
        print(line, type(line))
scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
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'>
>>> 
Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
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.

Member Avatar
snippsat
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 10 [?]
 
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
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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]
'''
Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
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 1 Year Ago by Lucaci Andrew, snippsat, pyTony and 3 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article