0

This is my nested list

nlist =[[1,"https://w.org",{'delay':0},"segment"],[2,"https://w1.org",{'delay1':60},"segment2"],    [3,"https://w.org",{'delay2:120'},"segment1"]]

I want to extract and do some operation as below how to do this in the pythonic efficient way for each list.

number = 1
timedelay = delay
url    = https://w{}.format(timedelay).org  #is delay add 0 in the format or add 60 in place
segment = segment

example :

 https://w0.org+'/'+number+segment # add 0 when delay 0
          https://w60.org+'/'+number+segment # add 60 when delay1 

Please note that adding key pair value as per the dictionary i can rearrange the URL.

and repeat this for all list .

  1. Requirement is if the timedelay is delay add 0 in the format or add 60 in place .This is something like choosing key value pair { 'delay':0 'delay':60} can this be also be put inside the list and a single for loop can be made to read the list and form URL and other variable assignment or should it be done only seperately in if loop after each of the list element is extracted?

  2. in such list ["a" ,[b,c,d],ab] how to access c and what is the index of it.

Marx Babu
  • 750
  • 3
  • 11
  • 34
  • 5
    What is the expected output? – Dani Mesejo Nov 02 '18 at 12:54
  • expected out put plus all the variable and form a URL where if delay add like .../0/ or .../60/ in the URL based on the delay,delay1,delay2 this data shall be in list ,if it can be as key value pair dictionary within the list it can be good. – Marx Babu Nov 02 '18 at 12:57
  • 6
    Can you format the expected output in the question body? a few examples? – Rocky Li Nov 02 '18 at 12:58
  • Added example .It would be good if i can add a key-value pair to the list. – Marx Babu Nov 02 '18 at 13:08
  • there is an mistake in your 3 items in your list : the dict must be {'delay2':120} and not {'delay2:120'} – Charles R Nov 02 '18 at 14:31

1 Answers1

1
for index, url_parts in enumerate(nlist):
    print("\n\nparam :\n", index, url_parts)

    url = url_parts[1] + '/' + str(url_parts[0]) + url_parts[3]
    # url_parts[2][next(iter(url_parts[2]))] -> get the first element of a dict
    url = url.replace('.org', str(url_parts[2][next(iter(url_parts[2]))])+'.org' )
    print("\nresult :\n", url)
Charles R
  • 1,621
  • 1
  • 8
  • 25
  • Thanks for the neet and clean code ,Could you just help me where can i get detailed reference on this part [next(iter(url_parts[2])) and what replace is doing here? – Marx Babu Nov 02 '18 at 16:46
  • 1
    I have found it here https://stackoverflow.com/questions/30362391/how-do-you-find-the-first-key-in-a-dictionary – Charles R Nov 02 '18 at 18:16
  • 1
    I wanted to write url_parts['delay'] but in the 2nd line I would have written url_parts['delay1'], etc... It could work if you have the key 'delay' for each line in your list. Do not hesitate to print things that are not clear to you. e.g. str(url_parts[2][next(iter(url_parts[2]))]) is equal to 0 for the first line, 60 for the second... – Charles R Nov 02 '18 at 18:21