-3

I want to print upto 50 elements in a list

i.e If there are 100 elements in the list I need first 50 alone. My current code for the above is

'Filter':switcheroo.get(zaxis,["None Selected"])[zaxis].unique().tolist()[:50]

If there are 25 elements in the list I need 25 alone. I get an error when the above code is used. from other posts I understand that the below code is the solution, but I cant understand how to implement it with my current code.'

[x for _, x in zip(range(n), records)]

How to take the first N items from a generator or list in Python?

Naren Murali
  • 19,250
  • 3
  • 27
  • 54
  • could you post some more of your code? – vcp Aug 22 '16 at 10:22
  • There is a pandas data frame which I am converting into a list, This list will be 10 to 100 elements long. Here I want to show upto 25 elements in the list. There is not much code I can provide for this, could you please take this as a generic question. – Naren Murali Aug 22 '16 at 10:25
  • Similar issue in http://stackoverflow.com/questions/5234090/how-to-take-the-first-n-items-from-a-generator-or-list-in-python. – prime Aug 22 '16 at 10:25
  • A list? a dataframe? question is unclear. What exactly are you trying to achieve? – DeepSpace Aug 22 '16 at 10:26
  • @ShashaGirl Yes I want to show upto 25 characters, I have provided the same link in my question, but I am unclear on how to implement the code which I have highlighted to a list – Naren Murali Aug 22 '16 at 10:29
  • @DeepSpace A data frame is first getting converted to a list by the code "dataframe["Column"].unique().tolist()" then the first 50 are taken by the code "[:50]", could you please tell me how to implement taking upto 50 elements? – Naren Murali Aug 22 '16 at 10:31
  • @NarenMurali Seems like you already have that code. What's wrong with `dataframe["Column"].unique().tolist()[:50]`? It should do exactly what you want it to. – DeepSpace Aug 22 '16 at 10:33
  • @DeepSpace will the same code work if the list is having only 10 elements? – Naren Murali Aug 22 '16 at 10:34
  • @NarenMurali Yes. you can try it and see what happens. – DeepSpace Aug 22 '16 at 10:35
  • Not sure why this question has negative score. It showed up when I searched for this problem and the solution helped me. – Cornelius Roemer Oct 27 '20 at 16:23

2 Answers2

8

first 50 elements of a list:

print mylist[:50]

Test:

newlist = [x for x in xrange(10)]
newlist2 = [x for x in xrange(100)]

print [x for x in newlist[:50]]
>[1,2,3,4,5,6,7,8,9]
print [x for x in newlist2[:50]]
>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
0
 #include <stdio.h>
 #include <string.h>
 struct arithmet {
 char  eval;
 int   first_num,sec_num;
  };

 /* function declaration */
 void eval_value( struct arithmet *valyu );
 int main( ) {

 struct arithmet valyu1;       
 struct arithmet valyu2;
 char oper;
 int num1,num2;
 gets(oper);
 scanf("%d",&num1);
 scanf("%d",&num1);
 valyu1.first_num=num1;
 valyu2.sec_num=num2;
 valyu1.eval=oper;
 eval_value(&num1);
 eval_value(&num2);
 eval_value(&per);

 return 0;
}
void eval_value(struct arithmet *valyu)
{
  valyu->first_num;
  valyu->sec_num;
  valyu->oper;
  if(oper=='+')
  {
    printf("%d",addit(int a,int b));
   }
  if(oper=='-')
  {
    printf("%d",subtractit(int a,int b));
   }
   if(oper=='*')
   {
        printf("%d",multiplyit(int a,int b));
   }
    if(oper=='/')
    {
        printf("%d",divideit(int a,int b));
    }
    if(oper=='@')
    {
       printf("%d",intdivideit(int a,int b));
    }
    if(oper=='%')
    {
        printf("%d",remdivideit(int a,int b));
    }
    if(oper=='~')
    {
        printf("%d",exponeit(int a,int b));
    }
 }
  • 1
    As mentioned on another (very similar) answer you recently posted, this may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. Thank you, and welcome to Stack Overflow! – Jeremy Caney May 06 '20 at 03:07