How to save data in lists (python) ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to save data in lists (python) ?

I don't know how to save data. For example... I want to store user input. Any ideas?

15th Feb 2017, 7:42 AM
lux arcadia
lux arcadia - avatar
16 Answers
+ 15
create list and take input from user and append to that list as much as u want ip = [] #blank list temp = input() ip.append(temp) #add to list print (ip)
2nd Mar 2017, 6:11 PM
Piyush Goyani
Piyush Goyani - avatar
+ 5
u can save it like a text file i had the same problem file = open("newfile.txt", "w") file.write("This has been written to a file") file.close()
15th Feb 2017, 10:30 AM
Elite
Elite - avatar
+ 3
Already answered here: https://www.sololearn.com/Discuss/217410/how-can-save-any-input-to-a-list Homework? All the classroom is here, or it's same personn for both account?
15th Feb 2017, 11:26 AM
visph
visph - avatar
+ 1
If I understood you well you wants to continue storing data in a list. The code below will continue to ask for input and store in a list until you enter the value True. myList = [] tester = True while tester: a = input('Enter Value: ') myList.append(a) if a == 'True': tester = False print(myList)
20th Mar 2017, 6:33 PM
Ahmad Abdulnasir Shuaib
Ahmad Abdulnasir Shuaib - avatar
0
I generally prefer to use the 'a' option when opening a file, as it means that the stuff that has been inputted is added to the file, rather than overwriting all the other data that is in the file already. For example, if you are going to save the results of a quiz or a game, then appending would be much more efficient. The only time I would use the 'w' option would be if I were going to update the data in a file, if there is only one set of it
21st Feb 2017, 10:43 AM
Bradley Beasley
0
If you want to store data as native python list object inside a file you can use standard module Pickle. Storing programming language objects inside binary files called serialization, reverse process called de-serialization. Example: import pickle some_list = [1,2,3] #store data inside "some_file.p" pickle.dump( some_list, open( "some_file.p", "wb" ) ) #load data from "some_file.p" other_list= pickle.load( open( "save.p", "rb" ) )
25th Feb 2017, 7:48 AM
Kilrog
Kilrog - avatar
0
Using pickle or json module. With json, it becomes even easier to store dictionaries data.
26th Feb 2017, 12:45 PM
Erick Mwenda Njagi
Erick Mwenda Njagi - avatar
0
quite fine
6th Mar 2017, 9:08 PM
Jazib Jamil
Jazib Jamil - avatar
0
you can create a While loop and store these data. data = [] i while True: i = input("Enter the values of data, 'stop' for end:") if i == "stop" : break data.append(i)
16th Mar 2017, 7:10 PM
Jessie
Jessie - avatar
0
list = [] listLen = 7 #just for fun for i in range(listLen): print("input ") list.append(input()) print(list)
17th Mar 2017, 1:47 PM
Yarden Akin
Yarden Akin - avatar
- 1
#Do you mean something like that : usr = input ("enter what you want to write :") file = open("file.txt", "w") file.write(usr) file.close() #If yes thank you verry much #if i did'nt understood explain me more
15th Feb 2017, 10:54 AM
lux arcadia
lux arcadia - avatar
- 1
you can use for loop, or while loop, and append this values
17th May 2017, 5:45 PM
Jessie
Jessie - avatar
- 2
jshbshjmd
19th Mar 2017, 4:47 PM
rekan
rekan - avatar
- 4
list_name = [ ] user_input = raw_input("Tell user to enter value") #i used raw_input() because of Python 2.7 list_name.append(user_input) print(list_name)
15th Mar 2017, 1:56 AM
ABHISHEK MISHRA
ABHISHEK MISHRA - avatar
- 4
inp = input() list1=list(inp)
19th Mar 2017, 3:35 PM
Maharzan Pushan
Maharzan Pushan - avatar
- 6
Use extension .phy👍
25th Feb 2017, 10:16 AM
Pawan Chaurasia
Pawan Chaurasia - avatar