3

So I have been trying to figure out how I can print out two different formats using one for loop. I would like to provide the code before explaining my issue

fullList = [
  {
    'url': 'www.randomsite.com/251293',
    'numbers': '7.5'
  },
  {
    'url': 'www.randomsite.com/251294',
    'numbers': '8'
  },
  {
    'url': 'www.randomsite.com/251295',
    'numbers': '8.5'
  },
  {
    'url': 'www.randomsite.com/251296',
    'numbers': '9'
  },
  {
    'url': 'www.randomsite.com/251297',
    'numbers': '9.5'
  }
]

#fullList = [
#  {
#    'numbers': '7.5'
#  },
#  {
#    'numbers': '8'
#  },
#  {
#    'numbers': '8.5'
#  },
#  {
#    'numbers': '9'
#  },
#  {
#    'numbers': '9.5'
#  }
#]

try:
    numbersList = []
    for numbers in fullList:
        numbersList.append('{}{}'.format('{}'.format(numbers.get('url') if numbers.get('url') else ''), numbers.get('numbers')))

    print(numbersList)
except Exception:
    pass

and what I am looking for outcome is:

If url is in the list: print('<url|numbers>') meaning the format would be <url|numbers>

If no url is in the list: print(numbers) and the print here should only give the numbers - I sometimes just want the numbers, meaning that in the list I removed all URL's so it will only remain numbers.

My problem is that I dont know how I can combine these two into one format. So far I am able to print out only numbers with the code I have provided.

3
  • why don't you use normal if/else to do this - it will be more readable. Commented Aug 1, 2019 at 9:07
  • @furas Hmm nothing I would thought about, Could you provide a example of how it would look etc? Would appreciate it alot! Commented Aug 1, 2019 at 9:08
  • 3
    instead of '{}'.format(something) you can use directly something Commented Aug 1, 2019 at 9:16

4 Answers 4

1

Use normal if/else. It will be more readable. And you have only one format.

for numbers in fullList:
    if numbers.get('url'):
        numbersList.append('{}|{}'.format(numbers.get('url'), numbers.get('numbers'))
    else:
        numbersList.append(numbers.get('numbers'))
Sign up to request clarification or add additional context in comments.

1 Comment

This was actually pretty more clear I would say and easier to configure if any changes for future :)
1

You can solve this problem and it will look more pythonic this way:

fullList = [
  {'url': 'www.randomsite.com/251293', 'numbers': '7.5'}, 
  {'url': 'www.randomsite.com/251294', 'numbers': '8'}, 
  {'url': 'www.randomsite.com/251295', 'numbers': '8.5'}, 
  {'url': 'www.randomsite.com/251296', 'numbers': '9'}, 
  {'url': 'www.randomsite.com/251297', 'numbers': '9.5'}, 
  {'numbers': '100'}
]


[(x['url'] + '|' + x['numbers']) if x.get('url') else x['numbers'] for x in fullList ]

You are using list comprehensions, minimizing nesting etc.

1 Comment

Right! This is wokring aswell as @furas example, Does the same thing just that Furas is using if else statements to be able to see the code more clear. However this works pretty well too :)
1

One solution is to select all values in each subdict and join them with a custom delimiter. In this way, you don't care if the key/value exist or not.

# Let's consider partial data
fullList = [
    {
        'url': 'www.randomsite.com/251293',
        'numbers': '7.5'
    },
    {
        'url': 'www.randomsite.com/251294',
        'numbers': '8'
    },
    {
        'url': 'www.randomsite.com/251295',
        'numbers': '8.5'
    },
    {
        'url': 'www.randomsite.com/251296',
    },
    {
        'numbers': '9.5'
    }
]


numbersList = []
for element in fullList:
    numbersList.append("|".join([element[v] for v in element.keys()]))

print(numbersList)
# ['www.randomsite.com/251293|7.5', 'www.randomsite.com/251294|8',
#     'www.randomsite.com/251295|8.5', 'www.randomsite.com/251296', '9.5']

You can do it in one line with list comprehension:

output = ["|".join([element[v] for v in element.keys()]) for element in fullList]
print(output)
# ['www.randomsite.com/251293|7.5', 'www.randomsite.com/251294|8', 
# 'www.randomsite.com/251295|8.5', 'www.randomsite.com/251296', '9.5']

Comments

1

Using list comprehension

Ex.

fullList = [
  {'url': 'www.randomsite.com/251293','numbers': '7.5'},
  {'url': 'www.randomsite.com/251294','numbers': '8'},
  {'url': 'www.randomsite.com/251295','numbers': '8.5'},
  {'url': 'www.randomsite.com/251296','numbers': '9'},
  {'url': 'www.randomsite.com/251297','numbers': '9.5'}
]

list1 = [ "{0}|{1}".format(x['url'],x['numbers']) for x in fullList ]
print(list1)

O/P:

['www.randomsite.com/251293|7.5', 'www.randomsite.com/251294|8', 'www.randomsite.com/251295|8.5', 'www.randomsite.com/251296|9', 'www.randomsite.com/251297|9.5']

OR

for the updated question, if the dictionary does not contain url

fullList = [
  {'url': 'www.randomsite.com/251296','numbers': '9'},
  {'numbers': '9.5'}
]
list1 = [ "{0}{1}".format((x.get('url')+'|' if 'url' in x else ''),x.get('numbers','')) for x in fullList ]
print(list1)

O/P:

['www.randomsite.com/251296|9', '9.5']

2 Comments

Hmm however the problem here is that if I now etc remove the URL's through the list, this would not give me a correct outprint, correct?
@Thrillofit86 If you know dictionary has to contain key then no need to use if else condition, you try with a simple list comprehension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.