0
#numerical_list is the list.

for each in numerical_list:
    x=each[0]
    x1=each[1]

    if x1 >= 1000:

        thousand_or_greater.append(x)

print (thousand_or_greater)

Can someone explain the for loop shown here ? Is there another alternate solution ?

Thank you.

CheckEm
  • 13
  • 3
  • at first, post our actual `numerical_list` – RomanPerekhrest Sep 18 '17 at 13:13
  • What part(s) of this don't you understand? – Scott Hunter Sep 18 '17 at 13:14
  • Possible duplicate of [List filtering: list comprehension vs. lambda + filter](https://stackoverflow.com/questions/3013449/list-filtering-list-comprehension-vs-lambda-filter) –  Sep 18 '17 at 13:14
  • @RomanPerekhrest the original list (numerical_list) was basically [['Casey', 176544.328149], ['Riley', 154860.66517300002], etc. and was just names followed by how many people had that name. – CheckEm Sep 18 '17 at 13:23

1 Answers1

0

You are looping over a list of some form of data structure with at least 2 indexes and if the 2nd index is greater than or equal to 1000, you are assigning the value of the first index to the list

thousand_or_greater = []
numerical_list = [['Casey', 176544.328149], ['Riley', 154860.66517300002]]

for each in numerical_list:
    x=each[0]
    x1=each[1]
    if x1 >= 1000:
        thousand_or_greater.append(x)

print (thousand_or_greater)
>> ['Casey', 'Riley']

This can be shortened using the following list comprehension:

[x[0] for x in numerical_list if x[1] >= 1000]
>> ['Casey', 'Riley']
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
  • [['Casey', 176544.328149], ['Riley', 154860.66517300002], etc. was the original numerical_list. It basically displayed names followed by how many people had that name. The question was to print or display only those names which were used by 1000 or more people. Thank you for the explaination. – CheckEm Sep 18 '17 at 13:19
  • It should still work if the data structure is a list of lists and your original solution works for the problem – Alan Kavanagh Sep 18 '17 at 13:20