-1

I can't figure out how to get only the first item from a list loop.

I am trying to get the first/next calendar item from exchangelib:

My code:

(...)

x = 5000 # minutes in future to look for calendar items

calendar = art(
    start=time,
    end=time + timedelta (minutes = x), 
)

for item in calendar:
    print(item.subject)

Output: All the calendar items in this period of time. What do I need to change in the last two lines of code if I want to get only the first/ next item?

Example Output:

First Line
Second Line
Third Line

Wanted Output:

First Line

Edit: Gained some more knowledge in the last few months, i can answer my own question now. And its very simple, I probably wrongly expressed myself and people misunderstood.

Solution:

for item in calendar:
    print(item.subject)
    break # a simple break so the loop only returns the first result
Biorez
  • 59
  • 8
  • Do you mean ["X1" "Y1" ....] as you final output you want in 1 list ? – Prashant Kumar Jan 30 '20 at 09:19
  • what are the input and the desired output? What is `item.subject`? Your post is confusing since you ask for `["X1" "X2"...]`, and you get that during the first split. So, what is exactly the issue? – alec_djinn Jan 30 '20 at 09:20
  • The input are multiple calendar items coming from exchangelib, I need only the first one from all the items. My problem is that with my code I am only getting the first word from every line instead of just the first line – Biorez Jan 30 '20 at 09:34
  • What is "item", and what is "xyz" ??? – bruno desthuilliers Jan 30 '20 at 09:47
  • "item" is a calendar item coming from exchangelib "xyz" is a defined period of time in which the script is looking for calendar items – Biorez Jan 30 '20 at 10:16

4 Answers4

0

List comprehension one-liner: items = [item.subject.split()[0] for item in xyz].

Which amounts to:

items = []
for item in xyz:
    items.append(item.subject.split()[0])
9769953
  • 10,344
  • 3
  • 26
  • 37
0

If you want to just modify and use your code, here is how you can get all the first values in 1 single list.

test = []
for item in xyz:            
    item_split=item.subject.split()
    test.append(item_split[0])
print(test)
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
  • Thanks for your help, i got the following error: TypeError: 'builtin_function_or_method' object is not subscriptable – Biorez Jan 30 '20 at 09:36
  • Sorry @Biorez. Typo with variable name. Fixed it now. Should work. – Prashant Kumar Jan 30 '20 at 10:35
  • This got really close to the final solution! Added solution to my code above. – Biorez Feb 04 '20 at 07:31
  • Great to hear it helped. Please upvote if you like. :) – Prashant Kumar Feb 04 '20 at 07:34
  • Just noticed one (hopefully small) mistake: I am not getting the first item, i am getting the last item... any idea? – Biorez Feb 04 '20 at 09:46
  • Yes. In your solution I see you have written `list.append(p)` outside the `for loop`. Put that line inside the loop and you will get all the values. Also, I think you might have to change `pass` with `continue`. – Prashant Kumar Feb 04 '20 at 10:13
  • Thanks for your help so far! Now I am getting all the items again... maybe an [0] array somewhere? – Biorez Feb 04 '20 at 10:17
  • I cannto write another answer its closed. You also need to include the 2 lines in my answer inside `for` loop after you have checked for `None` and written `continue`. Change the variable name from `list` to `test` in your solution. Never use keywords as your variable name. `list` is used for `LISTS` in Python. – Prashant Kumar Feb 04 '20 at 10:21
  • I have edited your solution in the question post. have a look at it. That should work fine. – Prashant Kumar Feb 04 '20 at 10:25
  • Now I'm getting the first word of every item in the list.. close shot – Biorez Feb 04 '20 at 10:34
  • Great I could be of help. :) Cheers – Prashant Kumar Feb 04 '20 at 10:50
  • Thanks so far! But thats not the whole solution yet.. I am getting the **first** word of **every line** now, but I need to get only the first line.. Example: `item.subject=[first test, second test, third test]` With your code the output is: `first second third` But i need: `First test` – Biorez Feb 04 '20 at 12:44
  • The output you are getting is what I was trying to solve as I did not have the sample input. Now, to get `first test` as your output why can't you just access the first element of the list with `item.subject[0]` as `item.subject` is a list as I can see. – Prashant Kumar Feb 04 '20 at 14:13
  • Sorry for the inconvinience.. item.subject[0] outputs just the first letter of every line Output: `f #(first) s #(second) t #(third)` item.subject[3] for example, outputs the fourth letter of every line Output: `s o r` – Biorez Feb 04 '20 at 14:59
0

If I have understood correctly, you want to split ONLY the first item on your list. Then you should simply access ONLY the first item (xyz[0]) and then split it. There is no need to iterate over the list.

print(xyz[0].subject.split())
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • Well that did something... I don't want to split only the first line, i figured out i need to use split to only **show** the first line and "split" the rest of it away. What your code did is just overwriting every item i get into the first one. So lets say I'm getting 3 items: X1 X2 X3 Y1 Y2 Y3 and Z1 Z2 Z3 with your code im getting X1 X2 X3 X1 X2 X3 X1 X2 X3 which is something – Biorez Jan 30 '20 at 10:23
  • Please clarify your question adding an example of the input. It's fundamental to answer properly. – alec_djinn Jan 30 '20 at 10:37
  • Added more code – Biorez Jan 30 '20 at 11:58
0

You are already getting ["X1", "X2", "X3", ...] and so on in your first step. You can do print(test[:1] in the first step only. Or you can also do xyz[0].subject.split(). This will not require any loops.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18