Python Forum
Arrange list of tuple using loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrange list of tuple using loop
#3
(Jun-16-2019, 10:05 AM)noisefloor Wrote: Hi,

first of all: your post would be _much_ more readable if you would write words correctly in upper and lower case and especially would use punctuation.

in Python you can iterate directly over list and tuples, as the are iterable. So no need for range or len or anything. Just do for item in iterable:.

I do not really understand what the final output should be. You a list of three tuples and want to get ... what? The example in your 2nd code block doesn't make to much sense... Do you want a dict for each item in the list, which would be three dicts as per your example. Or do you want to get a dict of dicts?
And why to do need a dict at all? Your input, the list of tuples, is already ordered, isn't it?
And did you check the documentation of your data source if this isn't able already to provide a dict instead of a list?

Regards, noisefloor

Hey, i want it to show as json by list, like this :

[(event_id :10, 
cp_ids : 'cp5',
event_type: 'ERROR',
start_ts : 201905021800.0,
end_ts : 201905021900.0,
record: 1.2, 1.5])
},
event_id :11,
cp_ids: 'cp3',
event_type : 'ERROR',
start_ts: 201905021800.0,
end_ts: 201905021900.0,
record: 1.2, 1.5
},
event_id:12,
cp_ids:'cp7', 'cp4',
event_type:'ERROR',
start_ts: 201905021800.0,
end_ts : 201905021900.0,
record: 1.2, 1.5
}
Even if i do the for i in, it does work but only for one list. the question is how do i make it
go throw all list ( if there is 3 lists like here or even 20 lists)

This is my code :
records = {
 
        "event_id": nvm[0][0] ,
        "cp_ids": nvm[0][1] ,
        "event_type": nvm[0][2] ,
        "start_ts": nvm[0][3] ,
        "end_ts": nvm[0][4] ,
        "record": nvm[0][5]
 
          }
How can i make the nvm[0][0] = nvm[n][0] to know to iterate on all list ?
Im new to python
Reply


Messages In This Thread
Arrange list of tuple using loop - by batchenr - Jun-16-2019, 08:05 AM
RE: Arrange list of tuple using loop - by batchenr - Jun-16-2019, 10:27 AM
RE: Arrange list of tuple using loop - by DeaD_EyE - Jun-16-2019, 11:19 AM
RE: Arrange list of tuple using loop - by batchenr - Jun-16-2019, 02:37 PM
RE: Arrange list of tuple using loop - by Yoriz - Jun-16-2019, 03:03 PM
RE: Arrange list of tuple using loop - by Abdullah - Jun-16-2019, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 560 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 534 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,779 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 2,044 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,343 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 107,196 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,919 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,531 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,938 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Appending to list of list in For loop nico_mnbl 2 2,415 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl

Forum Jump:

User Panel Messages

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