Python Forum
Using loop for getting data in several List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using loop for getting data in several List
#1
Hi every one,
I'm a newbie in using Python. I have 1 problem about list/ tuple/ dictionary/ set:
How can i get data in 10 list (/ tuple/ dictionary/ set) with the same name in beginning?.
For example:
I have 10 lists (Type: list) named: list1, list2,..., list10.
I want to get data in each list, so i used "For" loop.
For i in range(1,11)
  • i=1: I will get data in list1
  • i=2: I will get data in list2
  • ...
  • i=10: I will get data in list10
Can you show me how to do that? Thank you so much
Reply
#2
something like this:

for i in range (1, 11):
   list[i].append(whatever you want to add)
This will add to the end of whatever list selected... I think lol
Reply
#3
all_my_lists = [list1, list2, list3, list4, list5]
for one_list in all_my_lists:
    print(one_list)
Reply
#4
You should not create variable names dynamically, e.g. list1, list2, etc. Use a proper data structure like list or dict.
@michael1789's suggestion is in the right direction. However don't use list as variable name. It's a built-in function. His solution assumes you have created list of lists and only show how to append to elements (lists) in already existing list of lists.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
thanks all for your help. I've tried using listValue is a list that contains the other list. So i can get the data in each list by listValue.
Thank you so much again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 402 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Why changing data in a copied list changes the original list? plumberpy 3 2,223 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Appending to list of list in For loop nico_mnbl 2 2,350 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,368 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Loop files - Extract List Data To Individual Columns in CSV dj99 5 3,252 May-19-2019, 10:29 AM
Last Post: dj99
  loop through list or double loop 3Pinter 4 3,423 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,750 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,937 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  List data-member of object not updating inside loop. Sagar 2 3,397 Aug-30-2017, 01:07 PM
Last Post: Sagar
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,280 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020